我正在尝试为Google Spreadsheets中的特定工作表设置代码,代码设置为在特定工作表上工作,这可以通过数组完成,还是会产生问题,这就是我所拥有的:< / p>
原始代码:
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"));
}
}
这个电子表格由不同的代表处理,每个代表都有一个Sheet及其名称(以及最后一个句点来构建数组)所以当有人更新列时,KI希望将日期戳放在各自的工作表上的A列上,这样我无法完成它。
答案 0 :(得分:1)
是的,我认为这是可能的。这是一些工作代码。我不确定你想用正则表达式找到什么,所以你需要改变那一行。
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"));
}
}
}