這個問題在這里已經有了答案: 谷歌表格腳本在從表單插入的行上插入時間戳?如何使用 onChange 檢測插入的行? (3 個回答) 9 小時前關閉。
function onInsertColumn(activeRng) {
/* I coded this function, and it works, so I'll omit
the implementation here because it's irrelevant.
But I haven't figured out how to get the active range
from the onChange event (to pass to this function).*/
}
function onChange(event) {
// https://stackoverflow.com/a/66686524/470749
// https://stackoverflow.com/a/64454240/470749
// https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers
// https://developers.google.com/apps-script/guides/triggers/events
if(event.changeType == 'INSERT_COLUMN'){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet()
const activeRng = sheet.getSelection().getActiveRange(); // WHAT SHOULD GO HERE?
onInsertColumn(activeRng);
}
}
uj5u.com熱心網友回復:
解釋:
正如用戶在評論中已經提到的那樣,這取決于列的插入方式。如果這是通過電子表格 UI 手動完成的,那么您可以獲得所選活動列的編號:
const colNumber = sheet.getSelection().getActiveRange().getColumn();
然后創建一個range代表整個列范圍的物件:
const activeRng = sheet.getRange(1,colNumber,sheet.getMaxRows(),1);
那么您可以將此范圍提供給任何功能。
當然,getMaxRows()您也可以使用getLastRow()直到最后一行的內容而不是整個列范圍來代替。
手動插入列時的解決方法:
function onInsertColumn(activeRng) {
// example
activeRng.setValue("Selected");
}
function onChange(event) {
// https://stackoverflow.com/a/66686524/470749
// https://stackoverflow.com/a/64454240/470749
// https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers
// https://developers.google.com/apps-script/guides/triggers/events
if(event.changeType == 'INSERT_COLUMN'){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const colNumber = sheet.getSelection().getActiveRange().getColumn();
const activeRng = sheet.getRange(1,colNumber,sheet.getMaxRows(),1);
onInsertColumn(activeRng);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480310.html
上一篇:在谷歌表中使用查詢來呼叫某些行
