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

如何修改Google Apps Script生成PDF的输出位置?

问题解决:指定文件夹生成PDF(支持仅输出PDF)

问题根源

你代码里生成PDF的语句DriveApp.createFile(docblob)默认将文件创建在Google Drive根目录,没有关联到你指定的destinationFolder文件夹。只要调整调用方式,就能让PDF存入目标文件夹。

方案1:保留Doc+PDF并存入指定文件夹

修改生成PDF的代码段,调用目标文件夹的createFile方法即可。同时可选择把PDF链接写入表格:

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create Welcome Sheets', 'createNewGoogleDocs')
  menu.addToUi();
}

function createNewGoogleDocs() {
  const googleDocTemplate = DriveApp.getFileById('1xWGNF_3NwaYSGXbMOFS_xDBhVxK5QCM8JC4ABu402FA');
  const destinationFolder = DriveApp.getFolderById('18SPZN_wkwtWJI5Smaem6VxnwXxEeSM0F');
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Welcome Sheet Export');
  
  const rows = sheet.getDataRange().getValues();
 
  rows.forEach(function(row, index){
    if (index === 0) return;
    if (row[6]) return;

    const copy = googleDocTemplate.makeCopy(`${row[2]}` , destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();
    
    body.replaceText('{{First Name}}', row[0]);
    body.replaceText('{{Last Name}}', row[1]);
    body.replaceText('{{Email Address}}', row[2]);
    body.replaceText('{{Username}}', row[3]);
    body.replaceText('{{Email Password}}', row[4]);
    body.replaceText('{{LDAP Password}}', row[5]);
    
    doc.saveAndClose();
    
    // 关键修改:调用目标文件夹的createFile方法,将PDF存入指定路径
    const pdfBlob = doc.getBlob().setName(doc.getName() + ".pdf");
    const pdfFile = destinationFolder.createFile(pdfBlob);
    
    // 可选:将PDF链接写入表格(替换原Doc链接)
    const pdfUrl = pdfFile.getUrl();
    sheet.getRange(index + 1, 7).setValue(pdfUrl);
    
    // 若需保留原Doc链接,启用以下两行
    // const docUrl = doc.getUrl();
    // sheet.getRange(index + 1, 7).setValue(docUrl);
  })
}

方案2:仅输出PDF(不保留生成的Doc文件)

如果核心需求只是导出PDF,可在生成PDF后删除临时创建的Doc文件,节省存储空间:

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create Welcome PDFs', 'createNewGooglePDFs')
  menu.addToUi();
}

function createNewGooglePDFs() {
  const googleDocTemplate = DriveApp.getFileById('1xWGNF_3NwaYSGXbMOFS_xDBhVxK5QCM8JC4ABu402FA');
  const destinationFolder = DriveApp.getFolderById('18SPZN_wkwtWJI5Smaem6VxnwXxEeSM0F');
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Welcome Sheet Export');
  
  const rows = sheet.getDataRange().getValues();
 
  rows.forEach(function(row, index){
    if (index === 0) return;
    if (row[6]) return;

    // 创建临时Doc文件
    const copy = googleDocTemplate.makeCopy(`${row[2]}_temp` , destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();
    
    body.replaceText('{{First Name}}', row[0]);
    body.replaceText('{{Last Name}}', row[1]);
    body.replaceText('{{Email Address}}', row[2]);
    body.replaceText('{{Username}}', row[3]);
    body.replaceText('{{Email Password}}', row[4]);
    body.replaceText('{{LDAP Password}}', row[5]);
    
    doc.saveAndClose();
    
    // 生成PDF并存入指定文件夹
    const pdfBlob = doc.getBlob().setName(`${row[2]}.pdf`);
    const pdfFile = destinationFolder.createFile(pdfBlob);
    
    // 写入PDF链接到表格
    sheet.getRange(index + 1, 7).setValue(pdfFile.getUrl());
    
    // 删除临时Doc文件
    copy.setTrashed(true);
  })
}

关键修改点说明

  • 替换DriveApp.createFile(docblob)为destinationFolder.createFile(docblob),让PDF直接存入指定文件夹
  • 仅输出PDF版本通过copy.setTrashed(true)将临时Doc移入回收站,避免冗余文件
  • 可根据需求调整表格中写入的链接类型(Doc或PDF)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 02:20:25