当我填写B列时,我正试图让我的谷歌工作表填写A列中的日期,并让该功能在电子表格中的多个工作表上工作。
到目前为止,我有一个工作脚本,在我的第一张纸上工作。我已经尝试过几种方法,比如用变量创建一个数组(但这只会使最后一个数组名称起作用(,尝试用2个变量创建OR语句(但这会破坏它应该观察的列(。。。
你们能帮我吗?
这是适用于1张纸的名称:
function onEdit() {
// writes the current date to the cell to the right on the row when a cell in a specific column is edited
// adjust the following variables to fit your needs
var sheetNameToWatch = "2021";
var columnNumberToWatch = /* column A */ 2; // column A = 1, B = 2, etc.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var val=sheet.getActiveCell().getValue()
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && val!= "" /* I changed this var value so if I input anything, it will update the date */) {
var targetCell = sheet.getRange(range.getRow(), range.getColumn()-1 /* changed this to "-1" so it will input to the left column */
);
targetCell.setValue("" + Utilities.formatDate(new Date(), "CET", "dd/MM/yyyy"));
}
}
演示可以在这里找到:https://docs.google.com/spreadsheets/d/1mLBJrx3VCCwtcfUk-q_1DhwZFdA1cdrqFcySfC0cAi0/edit?usp=sharing
只需对您提供的代码进行一次小的编辑:
function onEdit() {
// writes the current date to the cell to the right on the row when a
cell in a specific column is edited
// adjust the following variables to fit your needs
var columnNumberToWatch = /* column A */ 2; // column A = 1, B = 2, etc.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var val=sheet.getActiveCell().getValue()
if (range.getColumn() ==
columnNumberToWatch && val!= "") {
var targetCell = sheet.getRange(range.getRow(), range.getColumn()-1
);
targetCell.setValue("" + Utilities.formatDate(new Date(), "CET", "dd/M
M/yyyy"));
}
}
以下代码片段已被删除:
var sheetNameToWatch = "2021";
sheet.getName() == sheetNameToWatch &&
从技术上讲,您的代码已经很好了,我们只需要根据您的IF语句删除条件,因为SpreadsheetApp.getActiveSheet();
会检测到您正在处理的当前工作表。