电子表格跟踪器

时间:2018-05-22 22:48:35

标签: javascript google-apps-script google-sheets

我在使用两次发布结果的脚本时遇到问题,我已经查看了脚本,但我找不到问题。

该脚本获取"Timestamp""Cell address""Column label""Value entered"并将其发布在名为"Tracker"的工作表上,但会发布两次

function onEdit() {
  var sheetsToWatch = ['Responses'];
  // name of the sheet where the changelog is stored
  var changelogSheetName = "Tracker";

  var timestamp = new Date();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getActiveCell();
  var sheetName = sheet.getName();

  // if it is the changelog sheet that is being edited, do not record the change
  if (sheetName == changelogSheetName) return;

  // if the sheet name does not appear in sheetsToWatch, do not record the change
  var matchFound = false;
  for (var i = 0; i < sheetsToWatch.length; i++) {
    if (sheetName.match(sheetsToWatch[i])) matchFound = true;
  }
  if (!matchFound) return;

  var columnLabel = sheet.getRange(/* row 1 */ 1, cell.getColumn()).getValue();
  var rowLabel = sheet.getRange(cell.getRow(), /* column A */ 1).getValue();

  var changelogSheet = ss.getSheetByName(changelogSheetName);
  if (!changelogSheet) {
    // no changelog sheet found, create it as the last sheet in the spreadsheet
    changelogSheet = ss.insertSheet(changelogSheetName, ss.getNumSheets());
    // Utilities.sleep(2000); // give time for the new sheet to render before going back
    // ss.setActiveSheet(sheet);
    changelogSheet.appendRow(["Timestamp", "Cell address", "Column label", "Value entered"]);
    changelogSheet.setFrozenRows(1);
  }
  changelogSheet.appendRow([timestamp, cell.getA1Notation(), columnLabel, cell.getValue()]);
}

1 个答案:

答案 0 :(得分:0)

Google的方法文档似乎可以通过以下函数以编程方式检查是否存在其他用户的触发器:

function triggerLogger() {
  // Read installed triggers for the project.
  var triggers = ScriptApp.getProjectTriggers();
  var installedReport = {};
  triggers.forEach(function (t) { installedReport[t.getUniqueId()] = {
      event: t.getEventType(),
      calledFunction: t.getHandlerFunction(),
      source: t.getTriggerSource(),
      source_id: t.getTriggerSourceId() || "Time-based triggers have no source id."
  }});

  // Read "simple" triggers for the project by checking for globals that start with "on".
  var simpleReport = {};
  for (var thing in this)
    if (thing.indexOf("on") === 0 && thing.length > 2)
      simpleReport[String(thing)] = {def: this[thing]};

  var possibleSimple = Object.keys(simpleReport).length,
      message = "Trigger report: " + triggers.length + " installed";
  if (possibleSimple) message += ", " + possibleSimple + " possible simple triggers";
  message += ".";

  // Log to Stackdriver (so the report can be viewed sensibly).
  console.log({
    message: message,
    installed: Object.keys(installedReport).length ?
        installedReport : "No detected installed triggers.",
    simple: possibleSimple ?
        simpleReport : "No simple triggers used",
    reportRunAs: Session.getActiveUser().getEmail()
  });
}

但是getProjectTriggers()方法,尽管声称要获得所有当前项目的已安装触发器,但只会获得您的安装的文档触发器,即使您是文档的所有者

请注意,此行为是accepted as a bug(意味着有人,有一天会修复它)。如果您希望自己已经尽力加速该时间表,请加注该问题: where to star