Google脚本错误

时间:2013-09-17 03:32:40

标签: google-apps-script

function createAndSendDocument() {
// Create a new document with the title 'Hello World'
var ui = DocumentApp.getUi();
var response1 = ui.prompt('What should be Name of your Document', ui.ButtonSet.YES_NO);
var doc = DocumentApp.create(response1.getResponseText());

var response = ui.prompt('What should be content of your Document',  ui.ButtonSet.YES_NO);
// Add a paragraph to the document
var paragraph  = prompt("What should be content of your Document")
doc.appendParagraph(response.getResponseText());

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

// Get the URL of the document
var url = doc.getUrl();

// Get the email address of the user
var response2 = ui.prompt('What should be content of your Document', ui.ButtonSet.YES_NO)
var emailAddress = response2.getResponseText();

// Send the user an email with a link to the document
GmailApp.sendEmail(emailAddress,
                 'Hello from my first Google Apps Script!',
                 'Here is a link to a document created by my ' +
                 'first Google Apps Script: ' + url);
}

这是我输入的代码它显示错误“无法从此上下文调用DocumentApp.getUi()。(第3行,文件”代码“)” 代码中有什么问题。 请回复。

2 个答案:

答案 0 :(得分:6)

如果它在电子表格中,而不是文档中,则会出现此错误。

因此,您必须使用电子表格替代

function onOpen() {
   var ss = SpreadsheetApp.getActive();
   var items = [
      {name: 'First item', functionName: 'menuItem1'},
      null, // Results in a line separator.
      {name: 'Second item', functionName: 'menuItem2'}
   ];
   ss.addMenu('Custom Menu', items);
}

https://developers.google.com/apps-script/guides/menus

答案 1 :(得分:1)

该方法只能从包含文档的脚本中调用。电子表格或表单中包含的脚本或独立的脚本无权访问文档用户界面实例。