结合两个onEdit函数

时间:2017-12-27 12:26:27

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

以下2个代码执行以下操作:

第一个代码:创建一个名为(changeLog)的更改日志表,以跟踪我的团队在名为(Sheet1)的工作表中Google表格中每个单元格所做的更改。

第二代码:防止更新单元格。当用户编辑工作表上的单元格(名为changeLog)时,将根据帮助工作表上的相同单元格对其进行检查。如果帮助工作表上的值为空,则新值将存储在两个工作表上。如果帮助工作表上的值不为空,则将其复制到主工作表上的单元格中,从而有效地撤消更改(以保护数据)已经由changeLog表中的第一个代码输入了

问题:这两个代码都是onEdit函数,我怎样才能让它们一起工作?

(P.s项目触发器已经设置为onEdit,并且每个函数都是自己工作的)

第一个代码:

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"

  var sheetsToWatch = ['Sheet1', 'add more'];
  // 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 3 */ 3, cell.getColumn()).getValue();
  var rowLabel = sheet.getRange(cell.getRow(), /* column B */ 2).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(["Email","Timestamp", "Sheet name", "Cell address", "Column label", "Row label", "Value entered"]);
    changelogSheet.setFrozenRows(1);
  }
  changelogSheet.appendRow([Session.getActiveUser().getEmail(), timestamp, sheetName, cell.getA1Notation(), columnLabel, rowLabel, cell.getValue()]);
}

第二代码:

function onEdit() {


  var masterSheetName = "Changelog" // sheet where the cells are protected from updates
  var helperSheetName = "Helper" // sheet where the values are copied for later checking
  var firstDataRow = 1; // only take into account edits on or below this row
  var firstDataColumn = 1; // only take into account edits on or to the right of this column

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var masterSheet = ss.getActiveSheet();
  if (masterSheet.getName() != masterSheetName) return;

  var masterCell = masterSheet.getActiveCell();
  if (masterCell.getRow() < firstDataRow || masterCell.getColumn() < firstDataColumn) return;

  var helperSheet = ss.getSheetByName(helperSheetName);
  var helperCell = helperSheet.getRange(masterCell.getA1Notation());
  var newValue = masterCell.getValue();
  var oldValue = helperCell.getValue();

  if (oldValue == "") {
    helperCell.setValue(newValue);
  } else {
    masterCell.setValue(oldValue);
  }
}

1 个答案:

答案 0 :(得分:2)

重命名你的2个函数,并使1 onEdit:

#include <iostream>
using namespace std;
void largerThanN(int[], int size, int n);

int main()
{
    int size;
    cin >> size;
    int a[size];
    for(int i=0; i < size; i++)
        cin >> a[i];
    int n;
    cin >> n;
    largerThanN(a, size, n);
    return 0;
}

void largerThanN(int a[], int size, int n)
{
    for(int i = 0; i < size; i++)
    {
        if(a[i] > n)
            cout << a[i] << endl;
    }
}