【问题标题】:Combine two scripts into one Google Sheet将两个脚本合并到一个 Google 表格中
【发布时间】:2017-08-04 23:32:27
【问题描述】:

我从不同的帖子中获得了这两个脚本,但在合并它们时遇到了问题。

如果 Sheet1 中的状态列发生更改,则短脚本会插入一个时间戳,并且另一个脚本会生成一个更改日志来记录所做的更改。是否可以将这两者合并为一个,并且可以编辑更改日志以仅记录编辑状态列的时间?

提前致谢!

谷歌电子表格:https://docs.google.com/spreadsheets/d/1m61JTFIacGifU-UD9yIzXdhAYKiiZHN3PooyifRHVf0/edit?usp=sharing

时间戳脚本:

`function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  if (sheet.getName() == "Sheet1") {
    var r = e.source.getActiveRange();
    if (r.getColumn() == 3) {
      sheet.getRange(r.getRow(),r.getColumn()+1).setValue(new Date());
    }
  }
}

`

变更日志脚本:

 function onEdit() {
  // 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://productforums.google.com/d/topic/docs/7CaJ_nYfLnM/discussion

  // 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 = ['outcome overview', 'Sheet1', 'Another sheet'];
  // name of the sheet where the changelog is stored
  var changelogSheetName = "Changelog";

  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", "Sheet name", "Cell address", "Column label", "Row label", "Value entered"]);
    changelogSheet.setFrozenRows(1);
  }
  changelogSheet.appendRow([timestamp, sheetName, cell.getA1Notation(), columnLabel, rowLabel, cell.getValue()]);
}

【问题讨论】:

    标签: merge google-sheets timestamp changelog


    【解决方案1】:

    尝试重命名您的函数并仅添加一个onEdit

    function onEdit(e) {
      myFunction1(e); // call function #1
      myFunction2(); // call function #2
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 2021-04-24
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多