简体   繁体   English

如何使触发脚本在谷歌表格中工作?

[英]How to make trigger script work in google sheets?

My intention is to generate a random number on a specific cell everytime I select anything on my sheet.我的意图是每次我在工作表上 select 时在特定单元格上生成一个随机数。 So what should I do in order to make this code below work?那么我应该怎么做才能使下面的代码工作呢?

/**
 * The event handler triggered when the selection changes in the spreadsheet.
 * @param {Event} e The onSelectionChange event.
 */
function onSelectionChange(e) {
  // Set background to red if a single empty cell is selected.
  var range = e.range;
  if(range.getNumRows() === 1 
      && range.getNumColumns() === 1 
      && range.getCell(1, 1).getValue() === "") {
      range.set(RAND());
  }
}

when I execute the code above I just got error like this TypeError: Cannot read property 'range' of undefined (line 7, file "triggers")当我执行上面的代码时,我得到了这样的错误TypeError: Cannot read property 'range' of undefined (line 7, file "triggers")

this is my first time utilizing script tool in sheets这是我第一次在工作表中使用脚本工具

How about this answer?这个答案怎么样?

Modification points:修改点:

  • onSelectionChange is the function for the simple trigger. onSelectionChange是简单触发器的 function。 And e of onSelectionChange(e) is the event trigger.onSelectionChange(e) e e 是事件触发器。 When the event is fired, e has the value.当事件被触发时, e具有值。 So from your error message of Cannot read property 'range' of undefined , I thought that you might be directly run the function onSelectionChange at the script editor.因此,根据您的错误消息Cannot read property 'range' of undefined ,我认为您可能会在脚本编辑器中直接运行 function onSelectionChange In this case, the event object is not given.在这种情况下,没有给出事件 object。 By this, such error occurs.这样,就会发生这样的错误。
  • In your script, there is a modification point.在您的脚本中,有一个修改点。 I think that range.set(RAND());我认为range.set(RAND()); might be range.setValue(RAND());可能是range.setValue(RAND()); . .
  • I think that range.getCell(1, 1).getValue() === "" can be modified to range.isBlank() .我认为range.getCell(1, 1).getValue() === ""可以修改为range.isBlank()

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

/**
 * The event handler triggered when the selection changes in the spreadsheet.
 * @param {Event} e The onSelectionChange event.
 */
function onSelectionChange(e) {
  // Set background to red if a single empty cell is selected.
  var range = e.range;
  if(range.getNumRows() === 1 
      && range.getNumColumns() === 1 
      && range.isBlank()) {  // Modified
      range.setValue(RAND());  // Modified
  }
}
  • In order to run this function, please copy and paste above script to the script editor of Spreadsheet, and save the script.为了运行这个 function,请将上面的脚本复制并粘贴到电子表格的脚本编辑器中,并保存脚本。 And, please select a cell on the Spreadsheet.并且,请 select 电子表格上的一个单元格。 By this, onSelectionChange is run by the simple trigger.这样, onSelectionChange由简单的触发器运行。
  • In this modification, it supposes that the function RAND() works.在此修改中,假设 function RAND()有效。

References:参考:

Added:添加:

When you want to put the value of RAND() to the cell "A1" when the cell is selected, how about the following script?当您想在选中单元格时将RAND()的值放入单元格“A1”中,下面的脚本怎么样?

Sample script:示例脚本:

function onSelectionChange(e) {
  e.range.getSheet().getRange("A1").setValue(RAND());
}

Sample situation:样品情况:

As a sample situation, when this script is used, the following result can be seen.作为示例情况,当使用此脚本时,可以看到以下结果。

function onSelectionChange(e) {
  e.range.getSheet().getRange("A1").setValue(Math.random());
}

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM