gpt4 book ai didi

javascript - Google 电子表格上的时间戳

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

我正在尝试使代码适用于 Google 电子表格中的特定工作表,该代码设置为在特定工作表上工作,这可以通过数组完成吗?或者会产生问题,这就是我所拥有的:

原始代码:

function onEdit() {
// writes the current date to the last cell on the row when a cell in a specific column is edited
// adjust the following variables to fit your needs
var sheetNameToWatch = "Sheet1";
var columnNumberToWatch = 20;

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) {
var targetCell = sheet.getRange(range.getRow(), sheet.getLastColumn());
targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}

这是我一直在尝试的(目前不起作用)

function onEdit() {
var repArray = new Array();
var allSheets = ss.getSheets();
for (i in allSheets) {
if ((allSheets[i].getName()).match(/.*?\.$/))
{repArray.push(allSheets[i].getName());}
}
var sheetArray = [];
for (var j in repArray) {
var tempSheet = ss.getSheetByName(repArray[j]);
var sheetNameToWatch = "tempSheet";
var columnNumberToWatch = 11

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) {
var targetCell = sheet.getRange(range.getRow(), 1); // I choose 1 because the Date Stamp should be placed in Column A
targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "dd-MM-yy"));
}
}

此电子表格由不同的代表处理,每个代表都有一张包含其姓名的工作表(以及末尾的句点以构建数组),因此当有人更新 K 列时,我希望将日期戳放置在各自工作表的 A 列上,以便到目前为止我还没有能够完成它。

最佳答案

是的,我认为这是可能的。这是一些工作代码。我不太确定您想用正则表达式找到什么,因此您需要更改该行。

function onEdit() {

//declarations
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allSheets = ss.getSheets();
var regExp = "/.*?\.$/";
var repArray = new Array();
var columnNumberToWatch;
var sheet;
var range;
var targetCell;

//used my own regex for testing
//note sure what your expression needed to search for
regExp = "name"

//create array of sheets that match reg-exp
for (var i in allSheets) {
if ((allSheets[i].getName()).match(regExp)) {
repArray.push(allSheets[i].getName());
}
}

//if a rep sheet was changed in a certain column then
//put the current date in column A in the last used row
columnNumberToWatch = 11;
sheet = ss.getActiveSheet();
range = sheet.getActiveCell();
// array.indexOf(string) returns the index if the string is found
// if the string is not found it will return a value of -1
if (repArray.indexOf(sheet.getName()) > -1 && range.getColumn() === columnNumberToWatch) {
targetCell = sheet.getRange(sheet.getLastRow(), 1);
targetCell.setValue(Utilities.formatDate(new Date(), "GMT", "dd-MM-yy"));
}

//the function will exit at the return statement;
return;
//old code with the for loop was left in case you want it
//the code was modified so it works now

var sheetNameToWatch;
//loop through sheets in rep-array
for (var j in repArray) {
sheetNameToWatch = ss.getSheetByName(repArray[j]).getName();
columnNumberToWatch = 11;
sheet = ss.getActiveSheet();
range = sheet.getActiveCell();
Logger.log(sheetNameToWatch+columnNumberToWatch+sheet.getName()+range.getA1Notation());
//if a rep sheet was changed in a certain column then
//put the current date in column A in the last used row
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) {
targetCell = sheet.getRange(range.getRow(), 1);
targetCell.setValue(Utilities.formatDate(new Date(), "GMT", "dd-MM-yy"));
}
}
}

关于javascript - Google 电子表格上的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33836312/

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