有工作脚本,需要添加变量

时间:2018-05-31 12:58:38

标签: google-apps-script google-sheets

我有这个脚本适用于我的Google表格。当列b中的值更改为指定值时,整行将移动到另一个工作表。我希望能够指定多个值,将所有行发送到某个工作表。奖励将是(如果很简单)设置​​一个或多个变量将行移动到表2的位置,并且一个或多个变量将行移动到表3,

function onEdit() {
  // moves a row from a sheet to another when a magic value is entered in a column
  // adjust the following variables to fit your needs
  // see https://productforums.google.com/d/topic/docs/ehoCZjFPBao/discussion

  var sheetNameToWatch = "2018";
  var columnNumberToWatch = 2; // column A = 1, B = 2, etc.
  var valueToWatch = "Next Year";
  var sheetNameToMoveTheRowTo = "Archive";

  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());
  }
}

(信用:我没有写这个剧本,也不记得我在哪里找到它了)

1 个答案:

答案 0 :(得分:1)

更改此行

var valueToWatch = "Next Year";

var valuesToWatch = ["Next Year", "Last year"] //add values to array

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && valuesToWatch.indexOf(range.getValue())>-1) {

看看是否有效?