我正在寻找一种捕捉时间和方式的方法。更新/编辑单元格时的日期(或仅限日期)。
我在网上搜索时发现了一些教程,但我觉得它有点过时,它没有100%运行。这是我找到的代码
function capdatetime(event)
{
var timezone = "GMT+8";
var timestamp_format = "MM-dd-yyyy"; //Timestamp format
var updateColName = "Date Sent";
var sheet = SpreadsheetApp.getActiveSpreadsheet(); //Name of the sheet where you want to run the script
var actRng = SpreadsheetApp.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 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 of 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new date(), timezone, timestamp_format);
cell.setValue(date);
}
}
我对此进行了修改,以解决我遇到的一些错误,但有一个特殊的错误我无法理解。
有人可以解释并帮助我吗?谢谢,我还会继续寻找一些想法。
答案 0 :(得分:2)
这个怎么样?
function capdatetime(event)
{
var timezone = "GMT+8";
var timestamp_format = "MM-dd-yyyy"; //Timestamp format
var updateColName = "Name";
var timeStampColName = "LastUpdated";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sample'); //Name of the sheet where you want to run the script
var actRng = sheet.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 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 of 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}