Редактировать: Изменено, чтобы иметь ту же функцию onClick-to-open, что и в примере документа (без предупреждений о рекламных блоках), но с ОП необходимо в одной кнопке.Хотя идеальным решением было бы использовать javascript await, это работает.Использование свойств страницы AppMaker для этого вопроса не требуется.Однако я сохранил их для простоты вопроса.
Я более подробно остановлюсь на ответе Павла.Для ускорения вы можете создать документ и перейти по ссылке до создания содержимого.
Задайте имя, немедленно подтвердите, откройте ссылку, затем снова откройте сценарий и внесите изменения в этот документ.
Я использовал ответ Павла, изменил имя одной функции, добавил параметр и добавил одну функцию.Остальное является копией и вставкой из образца документа .
события onClick виджета
if (!widget.root.descendants.CreateDocFormPanel.validate()) {
return;
}
var pageWidgets = widget.root.descendants;
var props = widget.root.properties;
props.CreatingDoc = true;
props.DocumentUrl = null;
google.script.run
.withSuccessHandler(function(documentUrl) {
clearForm(pageWidgets);
props.DocumentUrl = documentUrl;
props.CreatingDoc = false;
var win = window.open(app.pages.DocumentSample.properties.DocumentUrl, '_blank');
win.focus();
})
.withFailureHandler(function(error) {
console.error(JSON.stringify(error));
props.CreatingDoc = false;
})
.createDoc(
pageWidgets.NameTextBox.value,
pageWidgets.ContentTextArea.value);
клиентский скрипт
/**
* Clears form widgets after creating a Google Doc.
* @param {Object} formWidgets - widgets of a form.
*/
function clearForm(pageWidgets) {
pageWidgets.NameTextBox.value = null;
pageWidgets.ContentTextArea.value = null;
}
серверный скрипт
/**
* Configures a Google Doc.
* @param {string} id - id of the Google Doc.
* @param {string} content - content to add to a Google Doc.
* @return {string} URL of the configured Google Doc.
*/
function configDoc(id, content) {
// Creating the document.
var doc = DocumentApp.openById(id);
var body = doc.getBody();
// Insert a document header paragraph.
var title =
body.insertParagraph(0, 'A Document Created by an App Maker App');
title.setHeading(DocumentApp.ParagraphHeading.HEADING1);
// Insert a paragraph with provided content.
body.insertParagraph(1, content);
// Example of bold text.
var boldText = body.appendParagraph('Example of bold text');
boldText.setBold(true);
// Example of italic text.
var italicText = body.appendParagraph('Example of italic text');
italicText.setItalic(true);
italicText.setBold(false);
// Example of colored text.
var coloredText = body.appendParagraph('Example of colored text');
coloredText.setItalic(false);
coloredText.setForegroundColor('#388e3c');
// Example of text with background color.
var textWithBackground = body.appendParagraph('Text with background color');
textWithBackground.setForegroundColor('#000000');
textWithBackground.setBackgroundColor('#4fc3f7');
// Add a paragraph with link with italic style.
var link = body.appendParagraph('Learn more about Document Service');
link.setLinkUrl(
'https://developers.google.com/apps-script/reference/document/');
link.setBackgroundColor('#ffffff');
link.setItalic(true);
doc.saveAndClose();
return doc.getUrl();
}
/**
* Creates a Google Doc.
* @param {string} name - name of the Google Doc.
* @param {string} content - content to add to a Google Doc.
* @return {string} URL of the created Google Doc.
*/
function createDoc(name, content) {
var doc = DocumentApp.create(name);
doc.saveAndClose();
configDoc(doc.getId(), content);
return doc.getUrl();
}
Дополнительную информацию можно найти в справочной документации DocumentApp .