当我使用onEdit函数时,我的代码停止工作了吗? [英] when I use onEdit function my code stop working?

查看:57
本文介绍了当我使用onEdit函数时,我的代码停止工作了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码使用myFunction()可以很好地工作,但是当我将其更改为onEdit并使用e.range和列号重新调整特定列中的工作时,它会停止

my code works well using myFunction() but just when I change it to onEdit and use e.range and column number to restrect the working within specific column it stops

function onEdit(e) {

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Timesheet & Feedback");

var cell = ss.getActiveCell();
var cellContent = cell.getValue();
var Manaf = CalendarApp.getCalendarById("manaf652@gmail.com");
var MustafaSmeer = CalendarApp.getCalendarById("8q65ner19m8uk8idmmkmrr2hvs@group.calendar.google.com");
var activeRow = ss.getActiveCell().getRow();
var data = ss.getRange("A" + activeRow + ":G" + activeRow).getValues();

var range = e.range;
var CellColumn = range.getColumn();

if (CellColumn == 2) {


    if (cellContent === "Mustafa Sameer") {
        MustafaSmeer.createEvent(data[0][0], data[0][2], data[0][2], {
            description: "First call " + data[0][0]
        });
        MustafaSmeer.createEvent(data[0][0], data[0][4], data[0][4], {
            description: "Second call " + data[0][0]
        });
        MustafaSmeer.createEvent(data[0][0], data[0][6], data[0][6], {
            description: "Third call " + data[0][0]
        })
    }
}
};

推荐答案

说明:

您的简单触发器没有访问CalendarApp的权限.它们以"LIMITED" authMode而不是"FULL"运行,就像您手动运行脚本时一样.

Explanation:

Your simple trigger doesn't have authorisation to access CalendarApp. They run with "LIMITED" authMode, not "FULL" like when you're running the script manually.

这就是为什么脚本无法正常运行的原因,一旦您将脚本从具有运行许可的基于手动/时间的触发器中取出,然后将其替换为简单的触发器,便失去了所需的授权成功运行.有关每种触发器类型的权限的更多详细信息,请参见下面的参考.

This is why your script isn't running properly anymore, as soon as you took it out of a manual/time based trigger with your permission to run, then replaced it with a simple trigger, it lost the authorisation it needs to run succesfully. Please see the references below for more details on the permissions of each trigger type.

  1. 简单触发器
  2. 事件对象
  1. Simple Triggers
  2. Event Objects


解决方案:

请改用可安装的onEdit()触发器.为此,请进入项目的触发器,添加一个新触发器,并将其设置为在编辑电子表格时运行.然后将要求您对脚本进行授权,这将使它能够按预期运行,因为您已明确授予其授权.


Solution:

Use an installable onEdit() trigger instead. To do this, go into your project's triggers, add a new trigger and set it to run when the spreadsheet is edited. You'll then be asked to authorise the script, this will allow it to run as you're expecting, because you have given it your authorisation explicitly.

注意:请确保重命名功能,以免收到任何虚假的额外触发器.

Note: Make sure to rename your function so that you don't get any spurious extra triggers.

  1. 可安装触发器

这篇关于当我使用onEdit函数时,我的代码停止工作了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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