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

如何用Google App Maker实现PDF邮件发送及团队驱动器存储

Got it, let's tackle these two Google App Maker scenarios step by step—since you mentioned no Cloud SQL access, we'll lean on Google Docs, Apps Script, and Drive API to make this work:

1. Sending Records as PDF via Email in Google App Maker

Since we can't use the Cloud SQL-based PDF examples, we'll use a Google Docs template to generate the PDF dynamically, then email it out. Here's the playbook:

  • Step 1: Create a Google Docs Template
    Make a Docs file with placeholders that match your App Maker record fields (e.g., {{CustomerName}}, {{OrderDate}}). Store this somewhere your App Maker service account can access (we'll cover permissions in the second scenario).

  • Step 2: Add a Trigger in App Maker
    Add a button to your record detail page, then bind it to a client-side script that calls a server-side function. Example client script:

    function sendRecordAsPdf(record) {
      google.script.run
        .withSuccessHandler(function() {
          app.showToast("PDF sent successfully!", "Success", 3000);
        })
        .withFailureHandler(function(error) {
          app.showToast("Error sending PDF: " + error.message, "Error", 5000);
        })
        .generateAndEmailPdf(record._key);
    }
    
  • Step 3: Write the Server-Side Apps Script
    In App Maker, go to Scripts > Server Scripts and add this function. It'll fetch the record, populate the Docs template, convert it to PDF, and send the email:

    function generateAndEmailPdf(recordKey) {
      // Fetch the record from your App Maker model
      var record = app.models.YourModel.getRecord(recordKey);
      
      // Replace with your Docs template ID and user email field
      var templateId = "YOUR_DOCS_TEMPLATE_ID";
      var userEmail = record.EmailField; // Match your model's email field
      
      // Copy the template to a temporary file
      var tempDoc = DriveApp.getFileById(templateId).makeCopy("Temp Record PDF");
      var body = DocumentApp.openById(tempDoc.getId()).getBody();
      
      // Replace placeholders with record data
      body.replaceText("{{CustomerName}}", record.CustomerName);
      body.replaceText("{{OrderDate}}", record.OrderDate.toLocaleDateString());
      // Add more fields as needed
      
      // Save and close the temporary doc
      DocumentApp.getActiveDocument().saveAndClose();
      
      // Convert to PDF
      var pdfBlob = tempDoc.getAs(MimeType.PDF);
      
      // Send email with PDF attachment
      GmailApp.sendEmail(
        userEmail,
        "Your Record PDF",
        "Attached is your requested record as a PDF.",
        { attachments: [pdfBlob] }
      );
      
      // Clean up the temporary doc
      DriveApp.getFileById(tempDoc.getId()).setTrashed(true);
    }
    
2. Merging Docs from Restricted Templates & Saving PDFs to Team Drive + Email Copy

This adds two key requirements: accessing a template in a restricted folder/Team Drive, and saving the final PDF to a Team Drive. Here's how to adjust the workflow:

  • First, Fix Permissions
    Your App Maker project uses a service account to interact with Google Workspace tools. To access the restricted template and save to the Team Drive:

    1. Find your App Maker service account email in Settings > General > Service Account.
    2. Go to the template's folder/Team Drive, share it with this service account email with Edit permissions.
    3. Do the same for the target Team Drive where you want to save the final PDFs.
  • Modify the Server-Side Script
    Update the previous script to save the PDF to the Team Drive instead of trashing it, and still send the email attachment. Here's the revised function:

    function generateMergeAndSaveToTeamDrive(recordKey) {
      var record = app.models.YourModel.getRecord(recordKey);
      var templateId = "YOUR_RESTRICTED_TEMPLATE_ID";
      var userEmail = record.EmailField;
      var targetTeamDriveId = "YOUR_TEAM_DRIVE_ID"; // Find this in the Team Drive's URL
      var targetFolderId = "YOUR_TARGET_FOLDER_IN_TEAM_DRIVE"; // Optional: specific subfolder
      
      // Copy template (service account has access now)
      var tempDoc = DriveApp.getFileById(templateId).makeCopy("Merged Record - " + record.CustomerName);
      
      // Populate template (same as before)
      var body = DocumentApp.openById(tempDoc.getId()).getBody();
      body.replaceText("{{CustomerName}}", record.CustomerName);
      body.replaceText("{{OrderDate}}", record.OrderDate.toLocaleDateString());
      DocumentApp.getActiveDocument().saveAndClose();
      
      // Convert to PDF
      var pdfBlob = tempDoc.getAs(MimeType.PDF);
      
      // Save PDF to Team Drive
      var targetFolder = DriveApp.getFolderById(targetFolderId);
      var savedPdf = targetFolder.createFile(pdfBlob).setName("Final Record - " + record.CustomerName);
      
      // Send email with PDF attachment
      GmailApp.sendEmail(
        userEmail,
        "Your Merged Record PDF",
        "Attached is your merged record. A copy has also been saved to our Team Drive.",
        { attachments: [pdfBlob] }
      );
      
      // Clean up temporary doc
      DriveApp.getFileById(tempDoc.getId()).setTrashed(true);
    }
    
  • Pro Tips

    • To find a Team Drive ID: Open the Team Drive, look at the URL—it's the long string after drive/folders/.
    • If you don't need a specific subfolder, you can use the Team Drive ID directly with DriveApp.getFolderById(targetTeamDriveId).
    • Test permissions first: Run the script manually via the Apps Script editor to ensure the service account can access the template and write to the Team Drive.

内容的提问来源于stack exchange,提问作者Johan W.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:33:33