首页 文章

如何在appmaker中创建一个按钮执行一些操作并在之后打开一个URL

提问于
浏览
0

如何有一个按钮,当用户点击它时,会根据记录的字段创建Google文档,并在创建文档后打开文档 .

我在appmaker中使用了文档示例,我可以创建Google文档,但是我找不到使用调用创建文档功能的相同按钮打开Google文档的URL(创建后)的方法 .

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

2 回答

  • 0

    Edit: 已更改为具有与文档示例相同的onClick-to-open功能(无广告块警告),但OP需要在一个按钮中 . 虽然理想的解决方案是使用javascript等待,但这是有效的 . 此问题不需要使用AppMaker页面属性 . 然而,为了简单起见,我保留了它们 .


    我将扩展帕维尔的答案 . 对于速度,您可以在构建内容之前制作文档并转到链接 .

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

    我使用了Pavel的答案,更改了一个函数的名称并添加了一个参数,并添加了一个函数 . 其余的是Document Sample的复制和粘贴 .

    小部件的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中找到 .

  • 1

    试试从这个answer借来的这个片段:

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

相关问题