简体   繁体   English

Google App Scripts - 如何根据另一个单元格清除多个单元格的内容?

[英]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.我试图检查列的值是否包含“No Show”一词,如果一个单元格具有该值,我需要它删除同一行上的一些单元格,例如,如果 I5 列的值是“No Show”,我需要清除单元格 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.当每行的“I”列的值为“ No Show ”时,您要清除“B”、“C”、“D”、“F”和“G”列的单元格值。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本实现此目的。

In this case, I would like to propose to use TextFinder and the method of clearContent() of Class RangeList.在这种情况下,我想建议使用 TextFinder 和 Class RangeList 的clearContent()方法。 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.请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中并设置您要使用的工作表名称,然后在脚本编辑器中运行 function myFunction

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.运行此脚本时,当每行的“I”列的值为“ No Show ”时,“B”、“C”、“D”、“F”和“G”列的单元格值将被清除。
  • About the search value of No Show , when you want to check No Show , no show and so on, please remove .matchCase(true) .关于No Show的搜索值,当要勾选No Showno show等时,请去掉.matchCase(true)

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App 脚本根据该单元格的字体颜色清除单元格值 - Google App script to clear cells values on the base of Font color of that cell 在谷歌应用程序脚本中,我将如何制作一个 function 从另一列中的所有相应单元格中减去一列中的所有单元格 - In google app scripts, how would I make a function that subtracts all the cells in one column from all the respective cells in another column 输入另一个单元格时清除单元格内容 - Clear cell content when another cell is entered 如何记录谷歌应用脚本列中所有单元格的值 - How to Log value of all cells in a column on google app scripts 根据另一个单元格的内容清除一个单元格 - clear a cell based on the contents of another cell 清除列表/工作表中特定单元格的内容,查看来自谷歌工作表中另一个列表/工作表的值 - clear specific cells' content of a list/sheet seeing values from another list/sheet in google sheets 如何根据谷歌表格中其他单元格的值更改单元格的颜色 - How to change the color of a cell based on the value of other cells in google sheets Google App脚本-如何从范围获取单元格对齐 - Google app scripts - how to get cell alignments from range 在 Google App Scripts 中处理单元格内的日期 - Working with Dates within cells in Google App Scripts 如何从 Google App Scripts 中的另一个工作表中提取数据验证 - How to pull data validation from another sheet in Google App Scripts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM