如何修改Google Sheets的onEdit脚本仅监听E列4-34行?
修改Google Sheets onEdit脚本,限制仅在E列第4-34行触发
只需在原有脚本的终止判断条件中添加行范围校验,即可实现仅当编辑操作发生在E列(第5列)的第4行至第34行时,脚本才会执行后续逻辑。
修改步骤
找到onEditFunction_函数中的终止判断语句,在原有条件中加入行范围的判断:
// 原判断语句 if( editedCol !== MONITORCOL || shExcluded === true ) return; // 修改后的判断语句 if( editedCol !== MONITORCOL || shExcluded === true || row < 4 || row > 34 ) return;
修改后的完整脚本
/** * Make corrections to source sheet from the target sheet * To allow users to make corrections without risk of editing the incorrect column in the source sheet * Author: Mr Shane * Version: 2023-04-11-01 ⇨ For: https://support.google.com/docs/thread/210353550 * @OnlyCurrentDoc */ function onEdit(e) { if(!e) throw Error ("Do not run this script from the Apps Script Editor. It runs automatically when you hand edit the spreadsheet."); onEditFunction_(e); } function onEditFunction_(e) { const TABSEX = ["Daily Pay","Instructions"]; // Sheets to EXCLUDE from monitoring for an event/edit. const MONITORCOL = 5; // Configure the column that will be monitored. const r = e.range; // Cell being edited. const value = e.value; // The value of the edited cell const row = r.rowStart; // Row of the edit. const editedCol = r.columnStart; // Column of the edit. const sh = r.getSheet(); // Sheet on which the edit was done. const sheetName = sh.getName(); // Name of the sheet on which the edit was done. const shExcluded = TABSEX.includes(sheetName); // TRUE if sheetName is included. // 新增行范围判断:仅允许第4-34行的编辑触发后续逻辑 if( editedCol !== MONITORCOL || shExcluded === true || row < 4 || row > 34 ) return; // Abort if edit is (a) not in monitored column, (b) on excluded sheet, or (c) outside row 4-34 const ss = e.source; const sheetNumber = parseInt(sheetName.match(/\d+/)[0]); // Extract the number from the sheetName, and parseInt. ss.getSheetByName("Daily Pay").getRange(row+0,sheetNumber+1).setValue(value); // Write the value to the applicable cell (with row & column offset correction). r.clear(); } /** END onEdit Script */
内容的提问来源于stack exchange,提问作者Ylli Thaqi
相关产品推荐
相关产品推荐

