2
votes

I am working on adding functionality to a Google Spreadsheet. The spreadsheet imports data from the web, and I have it all formatted nicely. One of my columns is a series of strings, formatted in one of two ways - String or *String * without the space (basically importing italics from the web).

I am trying to come up with a script that runs when I open my spreadsheet, that will:

  • Unhide all rows in my spreadsheet
  • Loop through the spreadsheet
  • Hide every row where column 2(B) begins with an Asterisk

I have the following:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("DoDH");
  sheet.showRows(1, sheet.getMaxRows());
  for(var i=1; i<sheet.getMaxRows()+1; ++i){
    if (sheet.getRange(i, 2).getValue().){
      sheet.hideRow(i)
    }
  }
}

I don't know how to access the string inside each cell, and how to access characters within the string. Thank you in advance for any help.

1

1 Answers

3
votes

Here is updated code. See comments to have na insight

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("DoDH");
  var maxRows = sheet.getMaxRows();

  //show all the rows
  sheet.showRows(1, maxRows);

  //get data from clumn B
  var data = sheet.getRange('B:B').getValues();

  //iterate over all rows
  for(var i=0; i< data.length; i++){
    //compare first character, if asterisk, then hide row
    if(data[i][0].charAt(0) == '*'){
      sheet.hideRows(i+1);
    }
  }
}