结合两个onEdit功能

以下2个代码执行以下操作:结合两个onEdit功能

区号1:创建一个名为(更新日志)更改日志表来跟踪我的团队确实在谷歌表每个单元格在一个叫做表的变化(工作表Sheet1)。

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

问题:这两个代码都是onEdit函数,我怎么让他们一起工作?

(诗篇项目触发已设定为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);

}

}

回答:

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

function onEdit() 

{

onEdit1(); // run 1-st

onEdit2(); // run 2-nd

}

以上是 结合两个onEdit功能 的全部内容, 来源链接: utcz.com/qa/265040.html

回到顶部