Google Apps文档开启无效 [英] Google Apps Document onOpen not working

查看:96
本文介绍了Google Apps文档开启无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个自动填充作者,文档名称,创建日期以及根据Word文档修改日期的Google文档。



我有一个脚本,它是当我在脚本编辑器中运行时,以及当我通过添加的菜单项触发,但不会触发onOpen时正常运行。



这是我访问info的方式:

  var document = DocumentApp.getActiveDocument(); 
var body = DocumentApp.getActiveDocument()。getBody();
var bodyText = body.editAsText();
var docID = document.getId();
var docName = document.getName();
var file = DocsList.getFileById(docID);
var docCreated = file.getDateCreated()。toString();
var docUpdated = file.getLastUpdated()。toString();

然后在文档中做一些查找替换。

  var docName = document.getName(); 

是否需要触发getName()方法?我希望避免用户不得不点击按钮更新文档。



感谢任何帮助。



Mitch

解决方案

您可以检查 onOpen()触发器的执行脚本在打开文档后立即打开脚本编辑器,选择View - Execution Transcript。



在这里,你会发现如下所示:

  [13-08-01 11:53:18:163 EDT] Document.getName()[0 seconds] 
[ 13-08-01 11:53:18:203 EDT]执行失败:找不到具有给定ID的项目,或者您无权访问它。 (第41行,文件代码)[总运行时间0.052秒]

根据了解触发器,简单触发器不应允许执行需要认证的服务。这个限制似乎不适用于 Document.getName(),特别是在 Document.getId()成功之后工作。存在此问题,请访问 Issue 3083 a>,并为它投票&接收更新。

您会发现DocsList方法调用中存在类似的问题,但它们未包含在 Google Apps脚本安全模型,因此它们被阻止并不令人惊讶。



作为解决方法,您可以让您的 onOpen()提醒用户设置文档变量,直到他们完成。

 函数onOpen(e){
DocumentApp.getUi()。createMenu('Menu')
.addItem('Set variables', 'setVars')
.addToUi();

var doneFirstRun = ScriptProperties.getProperty('doneFirstRun');
if(doneFirstRun == null){
var ui = DocumentApp.getUi();
ui.alert(设置文档变量\\\
\\\

+选择菜单 - 设置变量);



函数setVars(){
var ui = DocumentApp.getUi();
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
var bodyText = body.editAsText();
var docID = document.getId();
var url = document.getUrl();
var docName = document.getName(); //在onOpen中不起作用
var editors = document.getEditors(); //不能在onOpen
var viewers = document.getViewers(); //在onOpen
中无效var file = DocsList.getFileById(docID);
var docCreated = file.getDateCreated()。toString();
var docUpdated = file.getLastUpdated()。toString();

// ...在这里做实际工作

//完成设置文档变量 - 禁用提醒。
var doneFirstRun = ScriptProperties.setProperty('doneFirstRun',true);

ui.alert(已完成);
}


I am attempting to create a google document that autofills author, document name, date created and date modified as per a Word doc.

I have a script that is functioning when I run in the script editor and when I trigger via an added menu item but will not trigger onOpen.

This is how I am accessing the info:

  var document = DocumentApp.getActiveDocument();
  var body = DocumentApp.getActiveDocument().getBody();
  var bodyText = body.editAsText();
  var docID = document.getId();
  var docName = document.getName();
  var file = DocsList.getFileById(docID);
  var docCreated = file.getDateCreated().toString();
  var docUpdated = file.getLastUpdated().toString();

I then just do some find replace in the document. Nothing seems to run after

var docName = document.getName();

Does the getName() method need to be triggered? I want to avoid users having to click a button to update the document.

Appreciate any assistance.

Mitch

解决方案

You can check the execution transcript of your onOpen() trigger function by opening the Script Editor shortly after opening the document, the selecting "View - Execution Transcript".

In there, you'll find something like this:

[13-08-01 11:53:18:163 EDT] Document.getName() [0 seconds]
[13-08-01 11:53:18:203 EDT] Execution failed: No item with the given ID could be found, or you do not have permission to access it. (line 41, file "Code") [0.052 seconds total runtime]

According to Understanding Triggers, simple triggers are not supposed to allow execution of services that require authentication. That restriction wouldn't seem to apply to Document.getName(), especially after Document.getId() has successfully worked. An issue exists for this, visit Issue 3083 and star it to vote & receive updates.

You'll find similar problems with your DocsList method calls, however they are not covered under the Google Apps Script Security Model, so it's not surprising that they get blocked.

As a work-around, you could have your onOpen() remind the user to set the document variables, until they've done so.

function onOpen(e) {
  DocumentApp.getUi().createMenu('Menu')
      .addItem('Set variables', 'setVars')
      .addToUi();

  var doneFirstRun = ScriptProperties.getProperty('doneFirstRun');
  if (doneFirstRun == null) {
    var ui = DocumentApp.getUi();
    ui.alert( "Set document variables\n\n"
             +"Select Menu - Set variables");
  }
}

function setVars() {
  var ui = DocumentApp.getUi();
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();
  var bodyText = body.editAsText();
  var docID = document.getId();
  var url = document.getUrl();
  var docName = document.getName();     // Does not work in onOpen
  var editors = document.getEditors();  // Does not work in onOpen
  var viewers = document.getViewers();  // Does not work in onOpen
  var file = DocsList.getFileById(docID);
  var docCreated = file.getDateCreated().toString();
  var docUpdated = file.getLastUpdated().toString();  

  // ... do actual work here

  // Completed setting document variables - disable reminder.
  var doneFirstRun = ScriptProperties.setProperty('doneFirstRun',true);

  ui.alert("Completed");  
}

这篇关于Google Apps文档开启无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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