简体   繁体   中英

Google App Scripts - How to clear multiple cells' content based on another cell?

Im trying to check a column's values for the words "No Show" and if a cell has that value I need it to delete some cells on the same row so for example if column I5 has "No Show" as its value I need to clear cells B5,C5,D5,F5,G5. I tried to write something but its not working and im relatively new to this. Thanks for the help

I believe your goal as follows.

  • You want to clear the cell values of the columns "B", "C", "D", "F" and "G", when the value of column "I" is No Show for each row.
  • You want to achieve this using Google Apps Script.

In this case, I would like to propose to use TextFinder and the method of clearContent() of Class RangeList. The sample script is as follows.

Sample script:

Please copy and paste the following script to the script editor of Google Spreadsheet and set the sheet name you want to use, and run the function myFunction at the script editor.

function myFunction() {
  const sheetName = "Sheet1"; // Please set the sheet name.

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const ranges = sheet
    .getRange("I1:I" + sheet.getLastRow())
    .createTextFinder("No Show")
    .matchCase(true)
    .findAll()
    .flatMap(r => {
      const row = r.getRow();
      return ["B", "C", "D", "F", "G"].map(e => e + row);
    });
  if (ranges.length > 0) sheet.getRangeList(ranges).clearContent();
}
  • When this script is run, the cell values of columns "B", "C", "D", "F" and "G" are cleared, when the value of column "I" is No Show for each row.
  • About the search value of No Show , when you want to check No Show , no show and so on, please remove .matchCase(true) .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM