當我填寫 B 列并讓該功能在電子表格中的多個作業表上作業時,我試圖讓我的 Google 作業表填寫 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
uj5u.com熱心網友回復:
只需對您提供的代碼稍作修改:
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();它將檢測您正在處理的當前作業表。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/369315.html
