onEdit(e)触发器对我不起作用-Google表格,Google Apps脚本 [英] onEdit(e) trigger doesn't work for me - Google Sheets, Google Apps Script

查看:81
本文介绍了onEdit(e)触发器对我不起作用-Google表格,Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在添加新名称或编辑现有名称后按名称的字母顺序对列进行排序(我从第二行开始了排序过程,因为第一行是我的标题).如图所示,我使用了onEdit(e)触发器.这应该是一件简单的事情,但是不起作用.当我在工作表中添加名称或编辑现有名称时,没有任何反应.

I just tried to sort a column with names alphabetically once a new name is added or an existing name is edited (I started the sorting process from row two because row one is my header). I used the onEdit(e) trigger as shown. It should be a simple thing to do but it does not work. When I add a name to the sheet or edit an existing one nothing happens.

ss = SpreadsheetApp.openById("1nQZT16wiN9AX6FqZhioKyeLWoL7_BKfi09zRg");
sheet = ss.getSheetByName("Sheet1");

function onEdit(e) {
  range = sheet.getRange(2, 1, sheet.getLastRow()-1);
  range.sort(1);  
}

电子表格的外观如下图所示:

The Spreadsheet looks like as seen in the picture:

我希望有人能解决这个问题.

I hope that someone has a solution to that problem.

推荐答案

您正在获取全球范围内的电子表格:

You're getting spreadsheet in global scope:

ss = SpreadsheetApp.openById("1nQZT16wiN9AX6FqZhioKyeLWoL7_BKfi09zRg");

这需要任何以下范围:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

但是简单的触发器未经授权即可运行 1 .因此,您不能使用SpreadsheetApp.openById().不过,您可以从事件对象 2 :

But simple triggers run without authorization1. Therefore, You cannot use SpreadsheetApp.openById(). You could however get the spreadsheet from the event object2 inside the on

const onEdit = e => e.source
  .getSheetByName("Sheet1")
  .getRange("A2:A")
  .sort(1)

如果要打开除当前绑定电子表格以外的任何其他电子表格,则需要使用可安装的触发器

If you want to open any other spreadsheet than the current bound one, you'd need to use installable triggers

这篇关于onEdit(e)触发器对我不起作用-Google表格,Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆