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

如何获取Google Sheets生成的Google Doc转PDF文件的URL?

问题分析

你遇到的核心问题是:temp.getAs(MimeType.PDF)返回的是Blob类型对象(仅包含文件二进制内容,并非Drive平台上的实体文件),Blob没有getUrl()方法,因此调用该方法会触发报错。而pdfFolder.createFile(pdfletter)执行后会返回Drive中实际创建的PDF文件对象,这个对象才支持getUrl()方法。

修复方案

修改PDF创建与URL获取的代码段:

// Folder with PDF archives
const pdfFolder = DriveApp.getFolderById('///');

// 生成PDF Blob并创建Drive文件,同时保存返回的File对象
const pdfFile = pdfFolder.createFile(temp.getAs(MimeType.PDF));

// 获取PDF文件的URL
const url = pdfFile.getUrl();

// 写入到表格对应列
sheet.getRange(index + 1, 7).setValue(url);
完整修复后的函数
function createNewGoogleDocs() {
    //This value should be the id of your document template that we created in the last step
    const googleDocTemplate = DriveApp.getFileById('***');
  
   //This value should be the id of the folder where you want your completed documents stored
   const tempFolder = DriveApp.getFolderById('+++');
   //Here we store the sheet as a variable
   const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Friends');
  
   //Now we get all of the values as a 2D array
   const rows = sheet.getDataRange().getValues();
  
      //Start processing each spreadsheet row
      rows.forEach(function(row, index){
     //Here we check if this row is the headers, if so we skip it
   if (index === 0) return;
    //Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
    if (row[6]) return;

    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder
    const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} template_letter`, tempFolder);

    //Once we have the copy, we then open it using the DocumentApp
    const temp = DocumentApp.openById(copy.getId());

    //All of the content lives in the body, so we get that for editing
    const body = temp.getBody();

    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    body.replaceText('{{Name}}', row[0]);
    body.replaceText('{{Email}}', row[1]);
    body.replaceText('{{Favorite_product}}', row[2]);
    body.replaceText('{{Date}}', row[3]);
    body.replaceText('{{SuscriptionDate}}', row[4]);
    body.replaceText('{{User}}', row[5]);

    
    //We make our changes permanent by saving and closing the document
    temp.saveAndClose();

    //Folder with PDF archives
    const pdfFolder = DriveApp.getFolderById('///');

    // 修复:将创建PDF文件的结果保存为File对象,再获取URL
    const pdfFile = pdfFolder.createFile(temp.getAs(MimeType.PDF));
    const url = pdfFile.getUrl();
    
    //Write that value back to the 'Document Link' column in the spreadsheet. 
    sheet.getRange(index + 1, 7).setValue(url);
  });
}
关键说明
  • temp.getAs(MimeType.PDF)生成的Blob仅代表文件的二进制数据,不具备Drive文件的属性(如URL、ID)
  • Folder.createFile(blob)方法会将Blob上传至Drive并返回对应的File对象,该对象拥有getUrl()、getId()等方法可获取文件的Drive平台属性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 11:33:26