简体   繁体   English

如何使用onEdit事件返回已编辑单元格的范围?

[英]How can I return the range of an edited cell using an onEdit event?

This seems like it should be easy as pie, but I can't seem to get any of my little test implementations to work. 这似乎应该很容易,但我似乎无法让我的任何小测试实现工作。 The ultimate goal is to create a function that runs only when cells in a certain range are edited. 最终目标是创建仅在编辑特定范围内的单元格时运行的函数。 To accomplish this, I am creating a function that runs on every edit but starts by checking to see if the range of the edited cell is within a certain range, and only executing the rest of the code if this is true, and otherwise, it just ends immediately. 为了实现这一点,我正在创建一个在每次编辑时运行的函数,但是首先检查已编辑单元格的范围是否在一定范围内,并且如果这是真的则仅执行其余代码,否则,它马上结束

The first step in doing this, it seems to me, is to find out how to return the range of the edited cell. 在我看来,这样做的第一步是找出如何返回已编辑单元格的范围。 I've tried various tests. 我尝试了各种测试。

Here is the current version, which I test by editing the spreadsheet: 这是当前版本,我通过编辑电子表格进行测试:

function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var eRange = e.range;
var message = eRange.getA1Notation();
ss.toast(message);
}

(output: toast does not appear/nothing happens) (输出:吐司不出现/没有发生)

I had about 10 other versions posted here, but I realized it was not helpful. 我在这里发布了大约10个其他版本,但我意识到它没有帮助。

I've managed to return [object Object] and [undefined] in my toast message, so I know I'm not making some silly error with my toast function. 我已经设法在我的toast消息中返回[object Object]和[undefined],所以我知道我的toast函数没有做出一些愚蠢的错误。

Doesn't e.range return a range? e.range不返回范围吗? Is that where I go wrong? 那是我出错的地方吗? If it returns a range, shouldn't I be able to use any of the methods in range, just as if I'd used something like "ss.getRange("A1").someMethod()"? 如果它返回一个范围,我不应该使用范围内的任何方法,就好像我使用了类似“ss.getRange(”A1“)。someMethod()”? Or is there some difference that I don't understand? 或者我有些不同,我不明白?

Okay, I realize I've asked a couple of different questions here-- I'm just trying to give a clearer picture of my thought process in case it helps you to correct me where I'm wrong. 好吧,我意识到我在这里提出了几个不同的问题 - 我只是想更清楚地了解我的思考过程,以防它帮助你纠正我错在哪里。 The simple question is this: what sort of command or method can I use to return the range of an edited cell? 简单的问题是:我可以使用哪种命令或方法来返回已编辑单元格的范围?

I'm so sorry to ask such a basic question. 我很遗憾地提出这样一个基本问题。 I'm really afraid that someone will enter the right combination of search terms and the answer will be right on the top of the results... but I just can't seem to find it on my own, so here I am. 我真的害怕有人会输入正确的搜索词组合,答案就会在结果的顶部......但我似乎无法自己找到它,所以我在这里。 I've tried lots of snippets of code from both StackOverflow and the old Google Groups support forum, as well as lots of my own attempts. 我已经尝试了StackOverflow和旧的Google网上论坛支持论坛的大量代码片段,以及我自己的很多尝试。 I see people have asked lots of similar questions regarding my ultimate goal here-- only running the onEdit function if cells in a certain range are edited-- but the answers are all more complex and seem tailored to each individual situation, and they don't fit mine. 我看到人们在这里问了很多关于我最终目标的类似问题 - 如果编辑了某个范围内的细胞,那么只运行onEdit功能 - 但答案更复杂,似乎适合每个人的情况,他们不会适合我的。 I'm certain the answer is out there, but I've been searching for three straight evenings now and found many similar questions asked, and just can't seem to get my own implementation of those suggestions to work. 我确定答案就在那里,但我现在已经连续三个晚上寻找并发现了许多类似的问题,而且似乎无法让我自己实施这些建议。 Thanks for being patient with me, and thank you so much for any answers you can offer! 感谢您对我的耐心,非常感谢您提供的任何答案!

here is the code taken (and slightly modified) from the doc : 这是从doc获取的代码(并稍加修改):

function onEdit(event){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  r.setComment("Last modified: " + (new Date())+' by '+Session.getActiveUser());
  ss.toast('Last cell modified = '+r.getA1Notation()+' by '+Session.getActiveUser());
}

暂无
暂无

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

相关问题 Function onEdit 的范围 object 当合并的单元格包含在编辑范围内时,事件不会返回正确的范围 - Function onEdit's range object Event does not return the correct range when merged cells are included in the edited range 如何使用特定范围的onEdit触发器构建脚本,在单元格中插入带有时间戳的注释? - How can I build script with an onEdit Trigger for a specific range, insert note in the cell, with timestamp? 基于编辑的单元格,我如何返回单元格所属的 namedRange? - based on the edited cell how do I return the namedRange the cell belongs in? 正在寻找可用于在编辑谷歌表格单元格时更改其颜色的 onEdit 脚本? - Looking for an onEdit script that can be used to change the color of a google sheets cell when it is edited? onEdit范围始终为单元格A1 - onEdit range always cell A1 如何使用 G-Suite 开发人员中心创建带有“来自电子表格”事件源的独立 onEdit(e) 触发器? - How can I create a stand-alone onEdit(e) trigger with a 'from Spreadsheet' event source using G-Suite Developer Hub? 如何使用onEdit触发器获取多个单元格范围的所有旧值 - How to get all old values for a mulitple cell range with onEdit trigger 为什么我的 onEdit 脚本在编辑时没有将单元格复制到相邻的单元格? - Why is my onEdit script not copying cell to adjoining cell when edited? 如何在编辑时将行移动到其他工作表并在单元格上打印时间戳? - How can I move rows to other sheets onEdit and print a Time Stamp on a cell? 如何限制我在 google 工作表中的 onEdit 函数的列(可能还有行)范围 - How can I restrict my range of columns (and maybe rows) for the onEdit function in google sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM