You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让App Maker按钮创建Google文档后直接打开文档链接

One-Click Document Creation & Opening in App Maker

Got it, let’s fix that two-click hassle and make your button handle both creating the Google Doc and opening it immediately. Here’s how to tie everything together:

Step 1: Update Your Server-Side Script

First, modify the function that creates your document to return the new document’s URL after creation. This lets your client-side code know where to redirect the user.

Replace your existing document creation function with something like this (adjust the template ID and field references to match your app):

function createDocumentAndGetUrl(record) {
  // 1. Define your template and document name (use your record's field here)
  const templateDocId = 'YOUR_TEMPLATE_DOCUMENT_ID';
  const newDocName = record.YourFieldName; // Replace with your actual field
  
  // 2. Create the document copy (same as your Document Sample logic)
  const templateDoc = DocumentApp.openById(templateDocId);
  const newDoc = templateDoc.makeCopy(newDocName);
  
  // 3. (Optional) If you need to populate the doc with record data, add that logic here
  // Example: Replace placeholders in the document with record values
  const docBody = newDoc.getBody();
  docBody.replaceText('{{Placeholder1}}', record.Field1);
  docBody.replaceText('{{Placeholder2}}', record.Field2);
  newDoc.saveAndClose();
  
  // 4. Return the URL of the newly created document
  return newDoc.getUrl();
}

Step 2: Wire Up the Button’s Client-Side Click Handler

Now, set your button’s onClick event to call the server-side function, then open the document once the URL is returned. Use google.script.run with a success handler to bridge server and client code:

function onButtonClick() {
  // Get the current record from the widget's datasource
  const currentRecord = widget.datasource.item;
  
  // Call the server-side function, then handle the result
  google.script.run
    .withSuccessHandler(function(docUrl) {
      // Open the new document in a new tab
      window.open(docUrl, '_blank');
    })
    .withFailureHandler(function(error) {
      // Show an error message if something goes wrong
      app.showSnackbar(`Oops, failed to create document: ${error.message}`);
    })
    .createDocumentAndGetUrl(currentRecord);
}

Key Notes

  • Permissions: Make sure your server-side function has the necessary permissions to create and modify Google Docs (App Maker usually handles this, but double-check if you run into authorization errors).
  • Field References: Replace YourFieldName, Field1, etc., with the actual field names from your App Maker record.
  • Template ID: Swap YOUR_TEMPLATE_DOCUMENT_ID with the ID of your Google Doc template.

This way, when the user clicks the button once, the document is created server-side, the URL is sent back to the client, and the document opens automatically in a new tab—no extra clicks needed!

内容的提问来源于stack exchange,提问作者Mauricio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:32:52