onOpen函数无法正确工作 [英] onOpen-function doesn't work correct

查看:97
本文介绍了onOpen函数无法正确工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本编辑器:用onOpen函数编写脚本(见下文)。使用脚本编辑器启动时,它可以正常工作。当电子表格打开时,我得到stmt 31Logger.log(name)的日志,没有其他事情发生。为什么?

以下是完整的脚本:

  function onOpen( ){
n0 =T22;
n1 = n0 +aktuell
n2 = n0 +主
var y1 = DayShift();
Browser.msgBox(返回+ y1);
};

函数DayShift(){
var dt = Browser.inputBox(Datum(YYMMDD)eingeben);
if(dt ==cancel|| dt.length!= 6)return(Input+ dt);

var f1 = GetFL(n1,0);
if(f1 ==cancel)return(F1 cancel);
Logger.log(f1);
var Rx = f1.getSheetByName(Sheet1)。getRange(B9)。getValue();
f1.rename(n0 + dt);
Logger.log(Rx);
Logger.log(f1.getName()+finished);

var f2 = GetFL(n2,1);
if(f2 ==cancel)return(F2 cancel);
Logger.log(f2);
f2.getSheetByName(Sheet1)。getRange(B7)。setValue(Rx);
Logger.log(f1.getName()+finished);

return(OK);
};

函数GetFL(name,typ){
Logger.log(name);
var fx = DocsList.find(name);
Logger.log(name +=+ fx.length);
if(fx.length!= 1)return(cancel);
if(typ == 1){
var fy = fx [0] .makeCopy(n1);
} else {
var fy = fx [0];
};
fy = SpreadsheetApp.openById(fy.getId());
Logger.log(fy);
return(fy);
};


解决方案

运行 onOpen 视为任何其他脚本。将其作为简单的触发函数运行将不允许访问DocsList API,因为这不能匿名运行。即使您已授权脚本以您的身份运行,但简单触发器将匿名运行,并且某些API不会允许此操作。



在这种情况下 onOpen (并且 onEdit )会自动失败。

它不会为你解决这个问题,但可以在 GAS文档中找到解释。



当您第一次遇到这种情况时,可能会感到沮丧/神秘,但即使它意味着从菜单命令或按钮运行脚本,也可以解决这个问题:脚本。


Script-Editor: wrote a script with the onOpen-function (see below). When started with the script-editor, it works correctly. when started when the spreadsheet is opened, I get the log of stmt 31 "Logger.log(name)", nothing else happens. Why?

Here is the complete script:

function onOpen() {
  n0="T22 ";
  n1=n0+"aktuell"
  n2=n0+"Master"
  var y1=DayShift();
  Browser.msgBox("Returned with "+y1);
};

function DayShift() { 
  var dt=Browser.inputBox("Datum (YYMMDD) eingeben");
  if (dt=="cancel" || dt.length !=6) return("Input "+dt);

  var f1=GetFL(n1,0);
  if (f1=="cancel") return("F1 cancel");
  Logger.log(f1);
  var Rx=f1.getSheetByName("Sheet1").getRange("B9").getValue();
  f1.rename(n0+dt);
  Logger.log(Rx);
  Logger.log(f1.getName()+" finished");

  var f2=GetFL(n2,1);
  if (f2=="cancel") return("F2 cancel");
  Logger.log(f2);
  f2.getSheetByName("Sheet1").getRange("B7").setValue(Rx);
  Logger.log(f1.getName()+" finished");

  return("OK");
};

function GetFL(name,typ) {
  Logger.log(name);
  var fx = DocsList.find(name);
  Logger.log(name+" = "+fx.length);
  if (fx.length != 1)  return("cancel");
  if (typ==1) {
    var fy=fx[0].makeCopy(n1);
  } else {
    var fy=fx[0];
  };
  fy=SpreadsheetApp.openById(fy.getId());
  Logger.log(fy);
  return (fy);
};

解决方案

running onOpen from the script editor treats as any other script. running it as a simple triggered function will not allow access to the DocsList API as this cannot be run anonymously. Even if you have authorised the script to run as you, simple triggers run anonymously and certain APIs will not allow this.

In this case onOpen (and onEdit) silently fail.

It won't fix this for you, but the explanation can be found in the GAS documentation.

It can be frustrating/mystifying when you first encounter this, but there are ways around it even if it means running scripts from menu commands or buttons: both confer identity of the user on the script.

这篇关于onOpen函数无法正确工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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