gpt4 book ai didi

google-apps-script - 修改某些特定列时插入 "LastUpdate Date"(脚本中的帮助)

转载 作者:行者123 更新时间:2023-12-04 04:54:08 25 4
gpt4 key购买 nike

情况:

我有一个包含 10 个工作表和 15 个用户登录并修改它的电子表格。

脚本功能:

当有人修改任何列中的任何行时,此脚本将使用 DateTime 更新 lastcolumn 并插入带有进行该修改的用户的注释。

问题:

(1) 性能问题:该脚本在用户修改任何列时运行。这个原因可能是当我有更多 > 3 个用户登录时,电子表格缓慢地保存。
(2) 仅当修改某些特定的列时才应运行此脚本。对于 ej.:如果事件用户修改了 A、B、C、D、E 列,则脚本会使用日期和时间更新最后一列 J;但是如果事件用户修改了 F、G、H 列,则不应运行该脚本。

测试用例:

(1) 该脚本在 OnEdit 上运行得很好,但是当有人修改任何列中的 anyrow 时,它会更新 lastcoulmn。

如果有人可以帮助我修改此脚本,以便仅在修改特定列时运行,我将不胜感激。

脚本:

    function onEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();

//Script Last Update Timming

var actSht = event.source.getActiveSheet();
var actRng = event.source.getActiveRange();

var index = actRng.getRowIndex();
var dateCol = actSht.getLastColumn();
var lastCell = actSht.getRange(index,dateCol);
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");

// Note: Insert the Date when someone update the row in the last coulmn
lastCell.setValue(date);

// Nota: Insert a comment in the lastCell with the user who modify that row
lastCell.setComment(Session.getEffectiveUser());
}

最佳答案

您可以使用以下代码检查事件行和事件列,然后根据识别的行、列,您可以进一步进行。

var activeCell = sheet.getActiveCell();
var row = activeCell.getRow();
var column = activeCell.getColumn();

你的整体代码看起来像这样。
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();

//Script Last Update Timming

var actSht = event.source.getActiveSheet();
var actRng = event.source.getActiveRange();

var activeCell = actSht.getActiveCell();
var row = activeCell.getRow();
var column = activeCell.getColumn();

if(row < 2) return; //If header row then return
var colNums = [1, 5, 6, 7]; //Coulmns, whose edit is considered
if(colNums.indexOf(column) == -1) return; //If column other than considered then return


var index = actRng.getRowIndex();
var dateCol = actSht.getLastColumn();
var lastCell = actSht.getRange(index,dateCol);
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");

// Note: Insert the Date when someone update the row in the last coulmn
lastCell.setValue(date);

// Nota: Insert a comment in the lastCell with the user who modify that row
lastCell.setComment(Session.getEffectiveUser());
}

关于google-apps-script - 修改某些特定列时插入 "LastUpdate Date"(脚本中的帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17015393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com