简体   繁体   English

如何将Google Apps脚本应用于整个Google表格电子表格?

[英]How to apply a Google Apps Script to an entire Google Sheets spreadsheet?

I'm trying to apply one script that will run on an entire Google Sheets spreadsheet, not just one tab. 我正在尝试应用一种脚本,该脚本将在整个Google表格电子表格中运行,而不仅仅是一个标签。 I have multiple tabs all with the same format; 我有多个标签都使用相同的格式; I want the script to look at the data validation in column F for every tab and if "Not Applicable" is selected for any row, move the contents of that entire row to a tab called "Not Applicable Items" 我希望脚本查看每个选项卡的F列中的数据验证,如果为任何行选择了“不适用”,请将整行的内容移至名为“不适用项”的选项卡

I have no experience writing scripts, so I copied the script I'm currently using from a forum topic. 我没有编写脚本的经验,因此我从一个论坛主题中复制了当前正在使用的脚本。 It successfully moves the rows to the correct tab, but only for the specified active sheet. 它成功地将行移动到正确的选项卡,但仅适用于指定的活动工作表。 I want the script to look at the entire spreadsheet and move any row marked "Not Applicable." 我希望脚本查看整个电子表格,并移动任何标记为“不适用”的行。

How can I do this? 我怎样才能做到这一点? Here's my code: 这是我的代码:

function onEdit() {
  var sheetNameToWatch = "Floorplan + Calendars";

  var columnNumberToWatch = 5; // column A = 1, B = 2, etc.
  var valueToWatch = "Not Applicable";
  var sheetNameToMoveTheRowTo = "Not Applicable Items";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();

  if (
    sheet.getName() == sheetNameToWatch &&
    range.getColumn() == columnNumberToWatch &&
    range.getValue() == valueToWatch
  ) {
    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet
      .getRange(range.getRow(), 1, 1, sheet.getLastColumn())
      .moveTo(targetRange);
    sheet.deleteRow(range.getRow());
  }

All of the scripts in a given project have access to all of the sheets in the spreadsheet. 给定项目中的所有脚本都可以访问电子表格中的所有工作表。 Thats why we recommend that you write you onEdit(e) script like this: 这就是为什么我们建议您这样编写onEdit(e)脚本的原因:

function onEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()!="Sheet Name") return;

This limits the script from performing operations on unwanted sheets. 这限制了脚本对不需要的图纸执行操作。

However, in your case you want all sheets involved in the onEdit(e) function so this is not necessary. 但是,在您的情况下,您希望所有表都包含在onEdit(e)函数中,因此这不是必需的。

Personally, I wouldn't run this function in an onEdit() function because it's liable to take longer than 30 seconds. 就个人而言,我不会在onEdit()函数中运行此函数,因为它可能需要30秒以上的时间。 But the functionality you requested is accomplished with the following script. 但是您要求的功能是通过以下脚本完成的。 exclA is an array of sheet names that you don't want to include in your "Not Applicable" search like the sheet named "Not Applicable Items". exclA是您不希望在“不适用”搜索中包含的工作表名称的数组,例如名为“不适用项目”的工作表。

function notApplicable() {
  var ss=SpreadsheetApp.getActive();
  var dsh=ss.getSheetByName('Not Applicable Items')
  var exclA=["Not Applicable Items"];
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++) {
    var name=shts[i].getName();
    if (exclA.indexOf(shts[i].getName())==-1) {
      var sh=shts[i];
      var rg=sh.getDataRange();
      var vA=rg.getValues();
      var d=0;
      for(var j=0;j<vA.length;j++) {
        if(vA[j][5]=="Not Applicable") {
          dsh.appendRow(vA[j]);
          sh.deleteRow(j+1-d++)
        }
      }
    }
  }
}

I think that a lot of users over use the onEdit() capability. 我认为很多用户都在使用onEdit()功能。

You can grab each spreadsheet in a for loop by doing 您可以通过以下方式在for循环中获取每个电子表格

for(var i = 0; i < [number of sheets]; i++){
  sheet = ss.getSheets()[i];

  . . . [rest of the code]
}

and include what you're trying to do inside the for loop to do with each sheet. 并在每个循环中将要尝试的内容包括在for循环中。

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

相关问题 如何使用Google Apps脚本(Google表格)打开未转换的Google电子表格 - How to open an unconverted google spreadsheet with google apps script (google sheets) 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets 如何将 Google Apps 脚本应用于多张工作表(相关下拉菜单) - How to apply Google Apps Script to multiple sheets (dependent drop downs) 如何将 Google Sheets &#39;HTTPResponse&#39; 应用于整个列? - How to apply Google Sheets 'HTTPResponse' to entire column? 电子表格中所有工作表上的setActiveCell。 Google Apps脚本 - setActiveCell on all sheets within a spreadsheet. Google Apps Script 自动填充公式(跨电子表格运行) - Google 表格/应用程序脚本 - AutoFill Formula (running across spreadsheet) - Google Sheets / Apps Script 使用Google Apps脚本列表框选择电子表格中的其他工作表 - Use Google Apps Script listbox to select different sheets in spreadsheet 使用 Google Apps 脚本激活电子表格的所有工作表 - Active all sheets of a spreadsheet using Google Apps Script 通过单元格引用更改电子表格名称 - Google Sheets / Apps Script - Change a SpreadSheet Name via a cell reference - Google Sheets / Apps Script Google Sheets / Apps Script:在特定文件夹中创建新电子表格的功能 - Google Sheets / Apps Script: Function to create a new spreadsheet in specific folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM