Manish S
Manish S

Reputation: 111

Clear Row Data Based on Value in Google Sheet ( Specific Data ) appscript

I am looking for a base on value clear specific row content/data and then hide row.

type value "Done" in { F Col } Clear Value of { E , C Col }

when everything is done then hide row.

enter image description here

function ClearContentThenHideRow() {
  var s = SpreadsheetApp.getActive().getSheetByName('Main');
  s.showRows(1, s.getMaxRows());
  
  s.getRange('F:F')
    .getValues()
    .forEach( function (r, i) {
    if (r[0] == 'Done') 
      //sheet.getRange(["C:C","E:E"]).clearContent(); // clear content not working 
      s.hideRows(i + 1);
    });
}

Upvotes: 0

Views: 121

Answers (1)

Tanaike
Tanaike

Reputation: 201358

Modification points:

  • About sheet.getRange(["C:C","E:E"]).clearContent();,
    • sheet is not declared.
    • When you want to use the range list, please use getRangeList().
  • When s.getRange('F1:F' + s.getLastRow()) is used instead of s.getRange('F:F'), I thought that the process cost will be able to be reduced a little.

When above points are reflected to your script, it becomes as follows.

Modified script:

function ClearContentThenHideRow() {
  var s = SpreadsheetApp.getActive().getSheetByName('Main');
  // s.showRows(1, s.getMaxRows()); <--- removed
  s.getRange('F1:F' + s.getLastRow())
    .getValues()
    .forEach(function (r, i) {
      if (r[0] == 'Done') {
        s.getRangeList(["C" + (i + 1), "E" + (i + 1)]).clearContent();
        s.hideRows(i + 1);
      }
    });
}

Reference:

Upvotes: 1

Related Questions