如何为Sheet的所有用户运行Google Script

时间:2015-09-23 17:00:59

标签: google-apps-script google-sheets google-spreadsheet-api

我已经构建了一个Sheet,它使用下面的脚本根据列中的选择将行从一个选项卡移动到另一个选项卡。例如,如果此列中的选择更改为CLOSED,则整行将移至CLOSED选项卡。如果将其更改为DEAD,则会将其移至DEAD选项卡。

问题是,它只在我这样做时才有效,但在任何其他协作者更改单元格时都不行。这是代码:

function onEdit(e) {
  var ss = e.source;
  var activatedSheetName = ss.getActiveSheet().getName();
  var activatedCell = ss.getActiveSelection();
  var activatedCellRow = activatedCell.getRow();
  var activatedCellColumn = activatedCell.getColumn();
  var activatedCellValue = activatedCell.getValue();

  var preclosingTrackerSheet = ss.getSheetByName("PRECLOSING"); // source sheet
  var closingTrackerSheet = ss.getSheetByName("CLOSING"); // target sheet
  var closedTrackerSheet = ss.getSheetByName("CLOSED FILES"); // target sheet
  var deadTrackerSheet = ss.getSheetByName("DEAD FILES"); // target sheet

  // if the value in column AG is "2-Closing", and you are in the PRECLOSING Sheet, MOVE the row to CLOSING sheet
  if (activatedSheetName == preclosingTrackerSheet.getName() && activatedCellColumn == 33 && activatedCellValue == "2-Closing") {
    // insert a new row at the second row of the target sheet
    closingTrackerSheet.insertRows(2,1);

    // copy the entire source row to the second row of target sheet
    var rangeToMove = preclosingTrackerSheet.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ preclosingTrackerSheet.getMaxColumns());
    rangeToMove.moveTo(closingTrackerSheet.getRange("A2"));

    // delete row from source sheet
    preclosingTrackerSheet.deleteRows(activatedCellRow,1);

  }

  // if the value in column AG is "CLOSED", and you are in the PRECLOSING Sheet, MOVE the row to CLOSED sheet
  if (activatedSheetName == preclosingTrackerSheet.getName() && activatedCellColumn == 33 && activatedCellValue == "3-Closed") {
    // insert a new row at the second row of the target sheet
    closedTrackerSheet.insertRows(2,1);
    // move the entire source row to the third row of target sheet
    var rangeToMove = preclosingTrackerSheet.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ preclosingTrackerSheet.getMaxColumns());
    rangeToMove.moveTo(closedTrackerSheet.getRange("A2"));

    // delete row from source sheet
    preclosingTrackerSheet.deleteRows(activatedCellRow,1);
  }


  // if the value in column AG is "DEAD", and you are in the PRECLOSING Sheet, MOVE the row to DEAD sheet
  if (activatedSheetName == preclosingTrackerSheet.getName() && activatedCellColumn == 33 && activatedCellValue == "x-Dead") {
    // insert a new row at the second row of the target sheet
    deadTrackerSheet.insertRows(2,1);
    // move the entire source row to the third row of target sheet
    var rangeToMove = preclosingTrackerSheet.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ preclosingTrackerSheet.getMaxColumns());
    rangeToMove.moveTo(deadTrackerSheet.getRange("A2"));

    // delete row from source sheet
    preclosingTrackerSheet.deleteRows(activatedCellRow,1);
  }

  // if the value in column AG is "PRECLOSING", and you are in the CLOSING Sheet, MOVE the row to PRECLOSING sheet
  if (activatedSheetName == closingTrackerSheet.getName() && activatedCellColumn == 33 && activatedCellValue == "1-Preclosing") {
    // insert a new row at the second row of the target sheet
    preclosingTrackerSheet.insertRows(2,1);

    // move the entire source row to the third row of target sheet
    var rangeToMove = closingTrackerSheet.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ closingTrackerSheet.getMaxColumns());
    rangeToMove.moveTo(preclosingTrackerSheet.getRange("A2"));

    // delete row from source sheet
    closingTrackerSheet.deleteRows(activatedCellRow,1);
  }

  // if the value in column AG is "CLOSED", and you are in the CLOSING Sheet, MOVE the row to CLOSED sheet
  if (activatedSheetName == closingTrackerSheet.getName() && activatedCellColumn == 33 && activatedCellValue == "3-Closed") {
    // insert a new row at the second row of the target sheet
    closedTrackerSheet.insertRows(2,1);
    // move the entire source row to the third row of target sheet
    var rangeToMove = closingTrackerSheet.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ closingTrackerSheet.getMaxColumns());
    rangeToMove.moveTo(closedTrackerSheet.getRange("A2"));

    // delete row from source sheet
    closingTrackerSheet.deleteRows(activatedCellRow,1);
  }


  // if the value in column AG is "DEAD", and you are in the CLOSING Sheet, MOVE the row to DEAD sheet
  if (activatedSheetName == closingTrackerSheet.getName() && activatedCellColumn == 33 && activatedCellValue == "x-Dead") {
    // insert a new row at the second row of the target sheet
    deadTrackerSheet.insertRows(2,1);
    // move the entire source row to the third row of target sheet
    var rangeToMove = closingTrackerSheet.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ closingTrackerSheet.getMaxColumns());
    rangeToMove.moveTo(deadTrackerSheet.getRange("A2"));

    // delete row from source sheet
    closingTrackerSheet.deleteRows(activatedCellRow,1);
  }  

}

如何告诉脚本为此工作表上的所有用户运行?

1 个答案:

答案 0 :(得分:0)

你是否关心这个功能是作为编辑器运行还是只是像你一样运行?

要像您一样运行,该功能需要在installable trigger上进行设置,并以您的处理配额用完。然后,任何编辑器都可以激活它,而无需使用此路由进行授权。资源>>当前项目的触发器>>添加新的 - 链接>> myFunction>>来自电子表格>> onEdit>>保存。

建议在这种情况下将onEdit()重命名为您自己的函数名称,以便在编辑时不会触发两次。

要作为实际编辑而不是代表您运行,那么他们每个人都需要先以某种方式预先授权脚本。这可以通过单击自定义菜单项,单击侧栏附加组件中的按钮,从附加组件存储安装附加组件或通过脚本编辑器手动运行功能来实现。您现有的onEdit()simple trigger将随之触发。