如何让 appmaker 中的按钮执行一些操作并在之后打开 URL

How to make a button in appmaker perform some actions and open a URL after

我怎样才能有一个按钮,当用户单击它时,它会根据记录的字段创建一个 Google 文档,并且在创建文档后它还会打开该文档。

我在appmaker中使用了文档示例,我可以创建Google文档,但是我找不到打开Google文档的URL的方法(在它之后已创建)使用调用用于创建文档的函数的相同按钮。

现在,我采用了与 Document Sample 相同的方法,在应用程序中有一个单独的 link(它在创建文档后获取文档的 URL) .我不喜欢这个解决方案的地方是用户需要在两个不同的地方点击。

试试从这个 answer 中借用的这个片段:

google.script.run
  .withSuccessHandler(function(documentUrl) {
    ...
    var win = window.open(documentUrl, '_blank');
    win.focus();
    ...
  })
  .withFailureHandler(function(error) {
    ...
  })
  .createDoc(...);

编辑: 更改为具有与文档示例相同的点击打开功能(无广告拦截警告),但 OP 需要在一个按钮中.尽管理想的解决方案是使用 javascript await,但它确实有效。此问题不需要使用 AppMaker 页面属性。但是为了简单起见,我保留了它们。


我将详细说明 Pavel 的回答。为了速度,您可以制作文档并在构建内容之前转到link。

设置名称,立即提交,打开 link,然后重新打开脚本并对该文档进行更改。

我用了Pavel的回答,改了一个函数的名字加了一个参数,又加了一个函数。其余部分是 Document Sample.

的复制和粘贴

widget 的 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 reference documentation 中找到更多信息。