5
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 行,文件“代码”)”

代码中的问题是什么?

4

2 回答 2

10

If it is in a spreadsheet, not a document, it will give this error.

Therfore you have to use the spreadsheet alternative

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

于 2013-11-08T07:44:35.250 回答
1

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

于 2013-09-17T04:13:23.023 回答