无法从我的脚本中打开Goog​​le文档

时间:2016-09-04 03:16:30

标签: google-apps-script google-docs

所以......我尝试使用this book开始使用Google Apps脚本。

在最初的一个例子中,我遇到了一些障碍;我无法通过脚本打开Goog​​le文档,打印问候语并关闭它。

以下是相关代码:

function helloDocument() {
  var greeting = 'Hello World!';

  // Create DocumentApp instance
  var doc = DocumentApp.create('test_DocumentApp');

  // Write the greeting to a Google Document.
  doc.setText(greeting);

  // Close the newly created document
  doc.saveAndClose();
}

关于什么让我抱​​怨的任何想法?当我从脚本编辑器中选择要运行的功能时,它会显示一条正在运行的消息,然后是......没有。

2 个答案:

答案 0 :(得分:1)

你快到了。如果您正在运行独立脚本(例如,未绑定到文档),则下面的代码应该适合您:

function helloDocument() {
  var greeting = 'Hello World!';

  // Create new google doc
  var doc = DocumentApp.create('test_DocumentApp');

  // get the body of the document
  var body = doc.getBody();

  body.setText(greeting);

  // Save and close the newly created document
  doc.saveAndClose();
}

Eugene和其他海报所述,如果您尚未执行此操作,则需要确保在第一次运行脚本时授予足够的权限。以下是显示工作流程的屏幕截图:

Review Permissions

点击Review Permissions。如果您的弹出窗口被阻止(可能是广告拦截器?),那么您可能还需要允许此网站的弹出窗口。

enter image description here

点击Accept为您的脚本授予权限。

这将运行您的代码并在驱动器的根目录中创建一个文件。转到驱动器并搜索名为test_DocumentApp的文件,您应该看到带有问候语字符Hello World!的文档。

答案 1 :(得分:1)

您分享的代码是在Google云端硬盘的根文件夹中创建名为test_DocumentApp的文件,并在其中添加文本Hello World!。如果找不到,请转到 Google云端硬盘>最近来查看它们。看看具有类似实现的Official Google Documentation and Video

我不确定你的意思是“那么......没什么。”也许您期望的结果是实际看到它打开了Google文档的标签,设置文本并关闭它?