gpt4 book ai didi

google-apps-script - "Cannot call DocumentApp.getUi() from this context"错误

转载 作者:行者123 更新时间:2023-12-02 22:15:34 25 4
gpt4 key购买 nike

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);
}

这是我输入的代码,它导致了错误:

"Cannot call DocumentApp.getUi() from this context. (line 3, file "Code")"

代码中存在什么问题?

最佳答案

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

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

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

关于google-apps-script - "Cannot call DocumentApp.getUi() from this context"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18840885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com