简体   繁体   English

谷歌应用脚本 OnEdit 仅在一张表中的一个单元格上运行

[英]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.我相信你的目标如下。

  • You want to run the script when the cell "B5" is edited.您希望在编辑单元格“B5”时运行脚本。

In this case, how about the following modification?在这种情况下,如何进行以下修改? In this modification, the event object e is used.在此修改中,使用了事件 object e

From:从:

function onEdit(e) {
  let ss = SpreadsheetApp;

To:至:

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;

Note:笔记:

  • 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.如果发生与权限相关的错误,请考虑使用可安装触发器。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 onEdit Google Sheet 脚本只在一张纸上运行? - onEdit Google Sheet script runs only on one sheet? google sheet 中的 onedit 脚本仅适用于一个 function - onedit script in google sheet works for only one function 在谷歌表格中 - 如何应用 onEdit 触发器以仅在一张表格上的一个单元格上运行? - In google sheets - how do I apply an onEdit trigger to run on just one cell on one sheet? 如何启动工作表中仅一个特定单元格的Google脚本函数onEdit? - How to fire a Google Script function onEdit of just one particular cell in a sheet? 您如何使用“OnEdit”功能将谷歌表格宏设置为仅在一张纸上运行? - How do you set a google sheets macro with the "OnEdit" function to run only on one sheet? 仅在编辑单个单元格时触发 Google Apps 脚本 OnEdit 触发器 - Google Apps Script OnEdit Trigger to be triggered only when one single cell is edited onEdit事件仅适用于一张纸 - onEdit event applicable to only one sheet Google Apps Script - onEdit 使用数组将数据从一张纸复制到另一张纸 - Google Apps Script - onEdit using an Array to Copy data one sheet to another 谷歌脚本将特定列从一张纸复制到另一张纸上的最后一行 - Google Script to copy specific columns from one sheet to another sheets last row onEdit 将单元格值从一张纸复制并粘贴到另一张onEdit - copy and paste cell value from one sheet to another onEdit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM