首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过AppSheet在Google工作表中记录复制粘贴和编程更改的应用脚本

通过AppSheet在Google工作表中记录复制粘贴和编程更改的应用脚本
EN

Stack Overflow用户
提问于 2021-04-23 03:47:27
回答 2查看 218关注 0票数 0

我有一个主表,我的团队和我每天至少做两次改变。更改记录在changelog表中。

changelog工作表目前只记录onEdit事件,即手动编辑。问题是,大多数更改都是使用、AppSheet、和来自多个来源的复制粘贴进行的。帮助我捕捉所做的所有更改。

这是我一直在使用的代码。

代码语言:javascript
复制
function onEdit(e) {
  addchangelog(e);
}

function addchangelog(e) {
  // This script records changes to the spreadsheet on a "Changelog" sheet.
  // The changelog includes these columns:
  // "Timestamp", "Sheet name", "Cell address", "Column label", "Row label", "Value entered"
  // Version 1.1, written by --Hyde, 30 July 2014
  // See https://support.google.com/docs/forum/AAAABuH1jm07CaJ_nYfLnM/?hl=en&msgid=fBuBv7najJwJ&gpf=d/msg/docs/7CaJ_nYfLnM/fBuBv7najJwJ

  // edit the following lines to suit your needs
  // changes are only recorded from sheets listed below
  // escape regular expression metacharacters as in \. \$ \+ \* \? \( \) \[ \]
  // see http://en.wikipedia.org/wiki/Regular_expression
  // use '.+' to include all sheets

  var sheetsToWatch = ['Master', 'TODAY'];
  var changelogSheetName = "ChangeLog";
  var timestamp = new Date();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getActiveCell();
  var sheetName = sheet.getName();
  if (sheetName == changelogSheetName) return;
  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) {
    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(["Row label", "Timestamp", "Sheet name", "Cell address", "Column label", "Value entered"]);
    changelogSheet.setFrozenRows(1);
  }
  changelogSheet.appendRow([timestamp, sheetName, cell.getA1Notation(), columnLabel, rowLabel, cell.getValue()]);
}
EN

回答 2

Stack Overflow用户

发布于 2021-04-23 08:23:42

只有在手工编辑电子表格时,简单onEdit(e)触发器才会运行,而不是在编程更改电子表格时运行。

要捕获AppSheet应用程序的修改,请删除onEdit(e)函数并创建一个运行addchangelog(e)触发器

现有的代码可以按原样工作,也可能需要稍作修改。见事件对象

票数 0
EN

Stack Overflow用户

发布于 2021-05-06 09:09:09

如前所述,只有当onEdit用户对电子表格进行编辑时,触发器才会运行。因此,在这种情况下,不考虑应用程序和公式。

onChange也不会解决这个问题,除非您最终修改了Changelog表的结构。

一种可能的解决方案是使用基于时间的触发器,并最终以编程方式检查更改。

参考文献

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67223753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档