Google表格:触发脚本IF值

时间:2017-03-01 14:00:50

标签: google-sheets triggers

有人可以帮我这个剧本吗?

function HistoryTrigger() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 if (sheet.getName() == "Status") {
   var activeCell = sheet.getRange("G11");
     if (activeCell.getValue() ="OK") {
        recordHistory();

     }
   }
 }

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Historie");
  var source = sheet.getRange("A2:B2");
  var values = source.getValues();
  values[0][0] = new Date();
  sheet.appendRow(values[0]);
};

我希望触发器在Status!G11="OK"时启动,然后启动recordHistory()

1 个答案:

答案 0 :(得分:0)

尝试:

if (activeCell.getValue() =="OK") {

double ==是if语句的正确语法。

您可以使用onEdit运行此functoin:

function onEdit(e) {
  if (e.range.getSheet().getName() === 'Status') { HistoryTrigger(); }
}

每次用户更改内容时都会进行检查。它将在G11在表格“状态”上正常运行时运行。

要将所有内容组合在一起,我建议将e传递给HistoryTrigger

function onEdit(e) {
      HistoryTrigger(e);
    }


function HistoryTrigger(e) {

 var ss = e.range.getSheet();
 if (ss.getName() === 'Status') {
   var activeCell = ss.getRange("G11");
     if (activeCell.getValue() == "OK") {
        recordHistory();

     }
   }
 }

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Historie");
  var source = sheet.getRange("A2:B2");
  var values = source.getValues();
  values[0][0] = new Date();
  sheet.appendRow(values[0]);
};