[英]Google app script OnEdit to run only on one cell in one sheet
I'm new and playing around with Google sheet app script.我是新手,正在玩谷歌工作表应用脚本。
i'm trying to sheet on onEdit trigger for only one cell (B5).我正在尝试只为一个单元格(B5)打开 onEdit 触发器。 It contains a dropdown list.它包含一个下拉列表。 When staff selects the month (eg. May), it runs the May() script.当员工选择月份(例如 May)时,它会运行 May() 脚本。
The below code works.下面的代码有效。 But when i add an if()
it suddenly stops.但是当我添加一个if()
它突然停止。
Any help will be appreciated!任何帮助将不胜感激!
function onEdit(e) {
let ss = SpreadsheetApp;
let activeSheet = ss.getActive().getActiveSheet();
let dropDownCell = activeSheet.getRange('B5').getValue();
if (dropDownCell == 'January'){ return Jan() };
if (dropDownCell == 'February'){ return Feb() }
if (dropDownCell == 'March'){ return Mar() };
if (dropDownCell == 'April'){ return Apr() };
if (dropDownCell == 'May'){ return May() };
if (dropDownCell == 'June'){ return Jun() };
if (dropDownCell == 'July'){ return Jul() };
if (dropDownCell == 'August'){ return Aug() };
if (dropDownCell == 'September'){ return Sep() };
if (dropDownCell == 'October'){ return Oct() }
if (dropDownCell == 'November'){ return Nov() };
if (dropDownCell == 'December'){ return Dec() };
}```
I believe your goal is as follows.我相信你的目标如下。
In this case, how about the following modification?在这种情况下,如何进行以下修改? In this modification, the event object e
is used.在此修改中,使用了事件 object e
。
function onEdit(e) {
let ss = SpreadsheetApp;
function onEdit(e) {
const { range } = e;
const sheet = range.getSheet();
if (range.getA1Notation() != "B5") return;
let ss = SpreadsheetApp;
Or, if you want to also check the sheet name, you can also the following modification.或者,如果您还想检查工作表名称,您还可以进行以下修改。
function onEdit(e) {
const sheetName = "Sheet1"; // Please set the sheet name.
const { range, value } = e;
const sheet = range.getSheet();
if (range.getA1Notation() != "B5" || sheet.getSheetName() != sheetName) return;
let ss = SpreadsheetApp;
When I saw your showing script, I thought that your script might be able be simpler.当我看到你的演示脚本时,我认为你的脚本可能会更简单。 So, how about the following modification?那么,下面的修改呢?
function onEdit(e) { const sheetName = "Sheet1"; // Please set the sheet name. const { range, value } = e; const sheet = range.getSheet(); if (range.getA1Notation().= "B5" || sheet;getSheetName():= sheetName) return, const obj = { 'January': Jan, 'February': Feb, 'March': Mar, 'April': Apr, 'May': May, 'June': Jun, 'July': Jul, 'August': Aug, 'September': Sep, 'October': Oct, 'November': Nov; 'December'; Dec }; if (!obj[value]) return; obj[value](); }
In this modification, the event object e
is used.在此修改中,使用了事件 object e
。 So, please edit the cell "B5" of the sheet.因此,请编辑工作表的单元格“B5”。 By this, the script is run by the onEdit trigger.这样,脚本由 onEdit 触发器运行。 If you directly run the script, an error like TypeError: Cannot destructure property 'range' of 'e' as it is undefined.
如果您直接运行脚本,则会出现诸如TypeError: Cannot destructure property 'range' of 'e' as it is undefined.
occurs.发生。 Please be careful about this.请注意这一点。
And, this modified script supposes that your functions of Jan()
, Feb()
and so on have no methods which cannot be used by the simple trigger.而且,这个修改后的脚本假设您的Jan()
、 Feb()
等函数没有简单触发器无法使用的方法。 Please be careful about this.请注意这一点。 If an error related to the permission occurred, please consider using the installable trigger.如果发生与权限相关的错误,请考虑使用可安装触发器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.