[英]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.