将相同的函数两次应用不同的变量

时间:2019-05-06 05:03:21

标签: google-apps-script

我正在使用OnEdit函数为Google表格和即时消息编写代码,以使用更新日期更新列。 (如果编辑了名为NAME的列下的单元格,则将在“时间戳记A”单元格下创建时间戳记。此方法工作正常。问题是当我尝试将相同的规则应用于另外两列时。每当编辑单元格名称PROGRESS下的单元格时,我都试图在称为“时间戳记B”的单元格下制作一个时间戳。

在下面的代码对我来说可以很好地使用原始选项,而我尝试编写的代码不起作用。谢谢!!

function onEdit(event) {
    var timezone = 'GMT';
    var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format. var updateColName = "NAME"; var timeStampColName = "time stamp A"; var sheet = SpreadsheetApp.getActive().getSheetByName('work_sheet'); var updateColName2 = "PROGRESS"; var timeStampColName2 = "time stamp B";

    var actRng = event.source.getActiveRange();
    var editColumn = actRng.getColumn();
    var index = actRng.getRowIndex();
    var headers = sheet.getRange(4, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf(timeStampColName);
    var updateCol = headers[0].indexOf(updateColName);
    updateCol = updateCol + 1;

    if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! var cell = sheet.getRange(index, dateCol + 1); var date = Utilities.formatDate(new Date(), "GMT+2", timestamp_format); cell.setValue(date); }

    }

我尝试编写的代码:

function onEdit(event) {
    var timezone = 'GMT';
    var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format. var updateColName = "NAME"; var timeStampColName = "time stamp A"; var sheet = SpreadsheetApp.getActive().getSheetByName('work_sheet'); var updateColName2 = "PROGRESS"; var timeStampColName2 = "time stamp B";

    var actRng = event.source.getActiveRange();
    var editColumn = actRng.getColumn();
    var index = actRng.getRowIndex();
    var headers = sheet.getRange(4, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf(timeStampColName);
    var updateCol = headers[0].indexOf(updateColName);
    updateCol = updateCol + 1;
    var dateCol2 = headers[0].indexOf(timeStampColName2);
    var updateCol2 = headers[0].indexOf(updateColName2);
    updateCol2 = updateCol2 + 1;

    if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! var cell = sheet.getRange(index, dateCol + 1); var date = Utilities.formatDate(new Date(), "GMT+2", timestamp_format); cell.setValue(date); }

        if (dateCol2 > -1 && index > 1 && editColumn == updateCol2) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! var cell = sheet.getRange(index, dateCol + 1); var date = Utilities.formatDate(new Date(), "GMT+2", timestamp_format); cell.setValue(date); }
    }

0 个答案:

没有答案