[英]Is there a google spreadsheet trigger that fires after a tab is changed?
當Tab從Sheet1到Sheet2從Sheet1更改時,我正在嘗試找到觸發器的方法。 我認為 onChange 觸發器會這樣做,但事實證明它在添加新工作表或刪除列/行時起作用。
問候薩阿德
onSelectionChange
已於2020 年 4 月 22 日發布。
但這不能在發布當天使用。 但是現在,我可以確認這必須能夠使用。 這樣,在當前階段,您的目標可以使用 onSelectionChange 事件觸發器來實現。
示例腳本如下。
onOpen
就會運行,當前的工作表被放到 PropertiesService 中。onSelectionChange
的事件對象似乎沒有關於tab 變化的信息。 所以為了檢測tab的變化,我使用了PropertiesService。onSelectionChange
由 onSelectionChange 事件觸發器運行,並將 A1Notation 放入單元格。function onOpen(e) {
const prop = PropertiesService.getScriptProperties();
const sheetName = e.range.getSheet().getSheetName();
prop.setProperty("previousSheet", sheetName);
}
function onSelectionChange(e) {
const prop = PropertiesService.getScriptProperties();
const previousSheet = prop.getProperty("previousSheet");
const range = e.range;
const a1Notation = range.getA1Notation();
const sheetName = range.getSheet().getSheetName();
if (sheetName != previousSheet) {
range.setValue(`Changed tab from ${previousSheet} to ${sheetName}. ${a1Notation}`);
// When the tab is changed, this script is run.
} else {
range.setValue(a1Notation);
}
prop.setProperty("previousSheet", sheetName);
}
當使用上述腳本時,當單元格單擊和選項卡更改時可以檢測如下。
沒有觸發器可以執行此操作,因為它更改的電子表格中沒有任何內容。
更改用戶正在查看的選項卡是(幾乎)純粹的客戶端操作,不會修改后端中的任何屬性。
當您查看 Google 電子表格時,它們只是存儲的所有值、公式和腳本的 html 表示。 此 html 表示的一部分是工作表顯示為選項卡的事實。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.