简体   繁体   English

在非活动工作表上运行脚本

[英]running script on an Inactive sheet

I have been using the following script to move "Finished" columns from one sheet to another: 我一直在使用以下脚本将“完成”列从一张纸移动到另一张纸:

function onEdit(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Sheet1" && r.getColumn() == 15  && r.getValue() == 
"Finished") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Finished");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

Im trying to figure out how to get the script to run even if the sheet has not been opened or edited. 我试图弄清楚即使未打开或编辑工作表也如何运行脚本。 Im just not sure how to go about changing it to use a time trigger every minute or so. 我只是不确定如何将其更改为每分钟左右使用时间触发器。

There are two aspects to the change to a timed trigger from onEdit. 从onEdit更改为定时触发器有两个方面。
The first concerns revisions to the code, the second is the trigger details. 第一个是对代码的修订,第二个是触发器的详细信息。

Code
The code can't be re-used because the timed trigger doesn't provide the same event details as OnEdit. 该代码无法重复使用,因为定时触发器未提供与OnEdit相同的事件详细信息。
In addition, it is possible that several rows might be tagged "Finished" between each trigger event, and the code needs to respond to them all. 另外,有可能在每个触发事件之间将几行标记为“完成”,并且代码需要对它们全部进行响应。 lastly, each "finished" row can't be deleted as it is found, because this affects the row number of all remaining rows in the column. 最后,每条“完成的”行都无法删除,因为这会影响该列中所有剩余行的行数。

The following code would do the job: 以下代码可以完成这项工作:
Most of it will be familiar to the questioner. 发问者会很熟悉。 The main except is to keep a record of each row number that is moved to "Finished". 主要的例外是保留每个行号的记录,该记录已移至“完成”。 This is done by pushing the row number onto an array. 这是通过将行号压入数组来完成的。 Then after all data has been examined and moved, there is a small loop that takes the row numbers recorded in the array and deletes the relevant row. 然后,在检查并移动了所有数据之后,有一个小循环,该循环获取记录在阵列中的行号并删除相关行。 The loop works from the highest row number to the lowest; 循环从最高的行号到最低的行号工作; this is so that the deletion of a row does not affect the row number of any remaining rows to be deleted. 这样一来,删除一行不会影响要删除的任何剩余行的行号。


function so_53305432() {

    // set up the spreadsheet
    var ss = SpreadsheetApp.getActiveSpreadsheet();

    // identify source and target sheets
    var sourceSheet = ss.getSheetByName("Sheet1");
    var targetSheet = ss.getSheetByName("Finished");

    // get some variables to use as ranges
    var sourcelastRow = sourceSheet.getLastRow();
    var numColumns = sourceSheet.getLastColumn();
    var targetLastRow  = targetSheet.getLastRow();

    // get data from the Source sheet
    var sourceData = sourceSheet.getRange(1, 1, sourcelastRow, numColumns).getValues();

    // set up some variables
    var finishedRows = [];
    var i = 0;
    var x = 0;
    var temp = 0;


    // loop through column 15 (O) checking for value = "Finished"
    for (i = 0; i < sourcelastRow; i++) {

        // If value = Finished
        if (sourceData[i][14] == "Finished") {

            // define the target range and move the source row
            var targetLastRow  = targetSheet.getLastRow();
            var target = targetSheet.getRange(targetLastRow + 1, 1);
            sourceSheet.getRange(+i + 1, 1, 1, numColumns).moveTo(target);

            // keep track of the source row number.
            finishedRows.push(i);
        }
    }

    // set up variables for loop though the rows to be deleted
    var finishedLength = finishedRows.length;
    var startcount = finishedLength - 1

    // loop throught the array to delete rows; start with the highest row# first
    for (x = startcount; x > -1; x--) {
        // get the row number for the script
        temp = +finishedRows[x] + 1;
        // delete the row
        sourceSheet.deleteRow(temp);
    }

}

Trigger 触发
The trigger needs to be revised. 触发条件需要修改。 To do this: 去做这个:
1) Open the script editor, select Current Project Triggers. 1)打开脚本编辑器,选择“当前项目触发器”。 OnEdit should appear as an existing trigger with an event type of OnEdit. OnEdit应该显示为事件类型为OnEdit的现有触发器。
2) Change "Choose which function to run" to the new function, 2)将“选择要运行的功能”更改为新功能,
3) Change "Select Event Source" from Spreadsheet to "Time Driven". 3)将“选择事件源”从电子表格更改为“时间驱动”。
4) Select "Type of time based trigger" = "Minutes Timer". 4)选择“基于时间的触发器类型” =“分钟计时器”。
5) Select "Select Minute Interval" = , and select a time period and interval. 5)选择“选择分钟间隔” =,然后选择一个时间段和间隔。
6) Save the trigger, and then close the Trigger tab 6)保存触发器,然后关闭“触发器”选项卡

If "Every Minute" is found to be too often, then the Questioner could try "Every 5 minutes". 如果发现“每分钟”太频繁,则发问者可以尝试“每5分钟”。


之前触发


之后触发


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

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