简体   繁体   English

我怎样才能让这个脚本运行在编辑上

[英]How can i get this Script to run onEdit

I am trying to get this to run automatically, however with the current form it only works if i manually run the code. 我试图让它自动运行,但是使用当前形式它只有在我手动运行代码时才有效。

I have another code file in this sheet which also uses onEdit, how can I incorporate the same onEdit to this code or make it run whenever column G is selected "Complete" to then change column H to "shipped" 我在此表中还有另一个代码文件,它也使用onEdit,如何将相同的onEdit合并到此代码中,或者只要选择列G“完成”就将其运行,然后将列H更改为“已发送”

function addValidation() {

  // Get the spreadsheet and active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  // Get the data as an Array you can iterate and manipulate
  var data = sheet.getDataRange().getValues();

  var rule = SpreadsheetApp.newDataValidation().requireValueInList(["Complete"]).build();

  // Loop the sheet
  for(var i=0; i<data.length; i++) {


    if(data[i][11] == "Complete") {


      sheet.getRange(i+1, 8).clear().setDataValidation(rule);

    }  {
      sheet.getRange(i+1, 8).clearDataValidations().setValue("Shipped");
    }
  }
}

A couple things: 几件事:

In your if statement, you're not checking Column G - you're checking column K (11). if语句中,您没有检查G列 - 您正在检查K(11)列。 If you want to check G, you need to change your if to: 如果要查看G,则需要将if更改为:

if(data[i][6].toString() == "Complete") {
  ...
}

Your logic is backward and you're missing the else keyword to run the alternative. 你的逻辑是向后的,你错过了else关键字来运行替代方案。 As is, it will mark 'Shipped' if it doesn't match 'Complete.' 如果它与“完成” 匹配,它将标记为“已发货”。 Swap the statements to fill the cell values correctly. 交换语句以正确填充单元格值。

Also, string checking in a loop is a little tricky. 此外,循环中的字符串检查有点棘手。 Cell references as indices are objects , not strings. 单元格引用作为索引是对象 ,而不是字符串。 You need to convert your cell using the .toString() method before you can compare a straight string. 在比较直字符串之前,需要使用.toString()方法转换单元格。

Your whole script should read: 您的整个脚本应为:

  function addValidation() {

  // Get the spreadsheet and active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  // Get the data as an Array you can iterate and manipulate
  var data = sheet.getDataRange().getValues();  

  // Loop the sheet
  for(var i=0; i<data.length; i++) {

    // To change which columns you're testing, change the second value.
    // ColG = 6, not 11.
    // Convert data[i][6] using .toString() to compare the value
    if(data[i][6].toString() == "Complete") {

      // If it's "Complete," mark Col H as 'Shipped'
      sheet.getRange(i+1, 8).setValue("Shipped");

    } else {
      // If you want something else to happen, add that here.
    }
  }
}

The last step is to enable a trigger for the script. 最后一步是为脚本启用触发器。 If you name your function onEdit() , it will run by itself because editing is a simple script. 如果将函数onEdit() ,它将自行运行,因为编辑是一个简单的脚本。 Since it's called addValidation , you need to manually add the onEdit trigger in the UI. 由于它被称为addValidation ,因此您需要在UI中手动添加onEdit触发器。 Go to Edit > Current Project Triggers and click on "Add Trigger" in the bottom right. 转到编辑>当前项目触发器,然后单击右下角的“添加触发器”。 Follow the steps in the UI and the script should run itself. 按照UI中的步骤操作,脚本应自行运行。

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

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