我有下面的脚本可以很好地从我的电子表格生成 pdf,但是它将 pdf 保存在我的谷歌驱动器的根目录中。我怎样才能让它保存到特定的文件夹? 任何帮助将不胜感激。
*/
// Config
// ------
// 1. Create a GDoc template and put the ID here
var TEMPLATE_ID = '164rlZ1XNJovaQAO1-OF2fNLWNEhbqEImNCJe4Ohk7tY'
// 2. You can specify a name for the new PDF file here, or leave empty to use the
// name of the template or specify the file name in the sheet
var PDF_FILE_NAME = ''
// 3. If an email address is specified you can email the PDF
var EMAIL_SUBJECT = 'Quote Test'
var EMAIL_BODY = 'Test'
// Constants
// ---------
// You can pull out specific columns values
var FILE_NAME_COLUMN_NAME = 'File Name'
var EMAIL_COLUMN_NAME = 'Email'
/**
* Eventhandler for spreadsheet opening - add a menu.
*/
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu('Create Quote')
.addItem('Create Quote', 'createPdf')
.addToUi()
} // onOpen()
/**
* Take the fields from the active row in the active sheet
* and, using a Google Doc template, create a PDF doc with these
* fields replacing the keys in the template. The keys are identified
* by having a <<,>> either side, e.g. <<Name>>.
*
* @return {Object} the completed PDF file
*/
function createPdf() {
var ui = SpreadsheetApp.getUi()
if (TEMPLATE_ID === '') {
ui.alert('TEMPLATE_ID needs to be defined in code.gs')
return
}
// Set up the docs and the spreadsheet access
var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(),
copyId = copyFile.getId(),
copyDoc = DocumentApp.openById(copyId),
copyBody = copyDoc.getActiveSection(),
activeSheet = SpreadsheetApp.getActiveSheet(),
numberOfColumns = activeSheet.getLastColumn(),
activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns).getValues(),
headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues(),
columnIndex = 0,
headerValue,
activeCell,
ID = null,
recipient = null
// Replace the keys with the spreadsheet values and look for a couple
// of specific values
for (;columnIndex < headerRow[0].length; columnIndex++) {
headerValue = headerRow[0][columnIndex]
activeCell = activeRow[0][columnIndex]
copyBody.replaceText('<<' + headerValue + '>>', activeCell)
if (headerValue === FILE_NAME_COLUMN_NAME) {
ID = activeCell
} else if (headerValue === EMAIL_COLUMN_NAME) {
recipient = activeCell
}
}
// Create the PDF file, rename it if required, delete the doc copy
// and email it
copyDoc.saveAndClose()
var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'))
if (PDF_FILE_NAME !== '') {
newFile.setName(PDF_FILE_NAME)
} else if (ID !== null){
newFile.setName(ID)
}
copyFile.setTrashed(true)
if (recipient !== null) {
MailApp.sendEmail(
recipient,
EMAIL_SUBJECT,
EMAIL_BODY,
{attachments: [newFile]})
}
ui.alert('New PDF file created in the root of your Google Drive ' +
'and emailed to ' + recipient)
} // createPdf()
最佳答案
看起来 Google 脚本没有办法只移动文件,您要么需要 add it to a directory或make a copy将其放入您希望其最终所在的目录中。
你可以做类似的事情
Folder folder = DriveApp.createFolder("PDF");
folder.addFile(newFile);
或
Folder folder = DriveApp.createFolder("PDF");
newFile.makeCopy(folder);
然后您应该能够删除newFile,这样目录中就只剩下该文件了。这假设您想要使用现有目录,您可以通过按 ID 搜索目录来对已存在的目录执行类似的操作。或name .
关于javascript - 用于生成PDF的Google脚本,如何设置保存到特定文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35730068/