Google apps script 从工作表生成Google文档并动态重命名它们
使用此脚本,我可以首先生成Google文档,然后将其转换为电子表格所有行的PDF格式,并使用“文件名”列中的值重命名每个PDF,然后删除Google文档。Google apps script 从工作表生成Google文档并动态重命名它们,google-apps-script,google-sheets,google-docs,Google Apps Script,Google Sheets,Google Docs,使用此脚本,我可以首先生成Google文档,然后将其转换为电子表格所有行的PDF格式,并使用“文件名”列中的值重命名每个PDF,然后删除Google文档。 我只会生成谷歌文档,不会生成PDF (我已经注释了字符串copyFile.setTrashed(true)以保存Google文档,但是如何使用列文件名重命名它们,以及如何避免生成PDF 代码 var ss = SpreadsheetApp.openById("xxxx").getActiveSheet(); var TEMPLATE_ID =
我只会生成谷歌文档,不会生成PDF (我已经注释了字符串
copyFile.setTrashed(true)
以保存Google文档,但是如何使用列文件名重命名它们,以及如何避免生成PDF
代码
var ss = SpreadsheetApp.openById("xxxx").getActiveSheet();
var TEMPLATE_ID = ss.getRange("C2").getValue();
var RESULTS_FOLDER_ID = ss.getRange("D2").getValue();
var PDF_FILE_NAME = ''
var FILE_NAME_COLUMN_NAME = 'File Name'
var EMAIL_COLUMN_NAME = 'Email'
var DATE_FORMAT = 'dd/MM/yyyy';
function createNewDocInFolder(newName, RESULTS_FOLDER_ID) {
var fileID,fileJson,resource;
resource = {};
resource.title = newName;
resource.mimeType = MimeType.GOOGLE_DOCS;
resource.parents = [{ id: RESULTS_FOLDER_ID }];
fileJson = Drive.Files.insert(resource);
fileID = fileJson.id;//The id of the new Google Doc
return fileID;
}
function GeneraAttestati() {
var ui = SpreadsheetApp.getUi();
if (TEMPLATE_ID === '') {
ui.alert('TEMPLATE_ID needs to be defined in code.gs')
return
}
var templateFile = DriveApp.getFileById(TEMPLATE_ID)
var activeSheet = SpreadsheetApp.getActiveSheet()
var allRows = activeSheet.getDataRange().getValues()
var headerRow = allRows.shift()
var copyFile = templateFile.makeCopy();
var copyId = copyFile.getId()
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getBody();
var numberOfColumns = headerRow.length;
allRows.forEach(function(row) {
function makeDoc(copyBody, headerRow, activeRow, numberOfColumns) {
var body,
contentAsTxt,
newName,
newDocFileID,
newDoc,
headerValue,
activeCell,
ID = null,
recipient = null,
copyFile,
columnIndex;
for (columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
headerValue = headerRow[columnIndex]
activeCell = activeRow[columnIndex]
activeCell = formatCell(activeCell);
copyBody.replaceText('%' + headerValue + '%', activeCell)
if (headerValue === FILE_NAME_COLUMN_NAME) {
ID = activeCell
} else if (headerValue === EMAIL_COLUMN_NAME) {
recipient = activeCell
}
}
contentAsTxt = copyBody.getText();
newName = PDF_FILE_NAME !== '' ? PDF_FILE_NAME : ID;
if (RESULTS_FOLDER_ID !== '') {
newDocFileID = createNewDocInFolder(newName,RESULTS_FOLDER_ID);// Create the DOC file in the results folder
}
newDoc = DocumentApp.openById(newDocFileID);
body = newDoc.getBody();
body.setText(contentAsTxt);
newDoc.saveAndClose();
}
makeDoc(copyBody, headerRow, row, numberOfColumns);
})
ui.alert('New DOC files created')
return
function formatCell(value) {
var newValue = value;
if (newValue instanceof Date) {
newValue = Utilities.formatDate(
value,
Session.getScriptTimeZone(),
DATE_FORMAT);
} else if (typeof value === 'number') {
newValue = Math.round(value * 100) / 100
}
return newValue;
}
}
var ss=SpreadsheetApp.openById(“xxxx”).getActiveSheet();
var TEMPLATE_ID=ss.getRange(“C2”).getValue();
var RESULTS_FOLDER_ID=ss.getRange(“D2”).getValue();
变量PDF文件名=“”
变量文件名列名='文件名'
var EMAIL\u COLUMN\u NAME='EMAIL'
变量日期格式='dd/MM/yyyy';
函数createNewDocInFolder(新名称、结果\u文件夹\u ID){
var fileID、fileJson、resource;
资源={};
resource.title=newName;
resource.mimeType=mimeType.GOOGLE\u文档;
resource.parents=[{id:RESULTS\u FOLDER\u id}];
fileJson=Drive.Files.insert(资源);
fileID=fileJson.id;//新Google文档的id
返回fileID;
}
函数GeneraAttestati(){
var ui=SpreadsheetApp.getUi();
如果(模板ID==''){
ui.alert('code.gs'中需要定义模板\u ID')
返回
}
var templateFile=DriveApp.getFileById(模板ID)
var activeSheet=SpreadsheetApp.getActiveSheet()
var allRows=activeSheet.getDataRange().getValues()
var headerRow=allRows.shift()
var copyFile=templateFile.makeCopy();
var copyId=copyFile.getId()
var copyDoc=DocumentApp.openById(copyId);
var copyBody=copyDoc.getBody();
var numberOfColumns=headerRow.length;
allRows.forEach(函数(行){
函数makeDoc(copyBody、headerRow、activeRow、numberOfColumns){
var body,
ContentAsText,
新名字,
newDocFileID,
纽多克,
人头价值,
活性细胞,
ID=null,
收件人=空,
复制文件,
柱状指数;
对于(columnIndex=0;columnIndex
执行此操作的唯一方法是使用高级驱动器服务。您必须从“代码编辑器”中,从“资源”菜单和“高级谷歌服务”中启用它
一些变量从循环中移出,然后传入,以避免在每次迭代中一次又一次地重新分配相同的值
var EMAIL_COLUMN_NAME = 'Email',DATE_FORMAT = 'dd/MM/yyyy';
function createNewDocInFolder(newName, RESULTS_FOLDER_ID) {
var fileID,fileJson,resource;
resource = {};
resource.title = newName;
resource.mimeType = MimeType.GOOGLE_DOCS;
resource.parents = [{ id: RESULTS_FOLDER_ID }];
fileJson = Drive.Files.insert(resource);
fileID = fileJson.id;//The id of the new Google Doc
return fileID;
}
function GeneraAttestati() {
var ui = SpreadsheetApp.getUi();
if (TEMPLATE_ID === '') {
ui.alert('TEMPLATE_ID needs to be defined in code.gs')
return
}
var templateFile = DriveApp.getFileById(TEMPLATE_ID)
var activeSheet = SpreadsheetApp.getActiveSheet()
var allRows = activeSheet.getDataRange().getValues()
var headerRow = allRows.shift()
var copyFile = templateFile.makeCopy();
var copyId = copyFile.getId()
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getBody();
var numberOfColumns = headerRow.length;
allRows.forEach(function(row) {
function makeDoc(copyBody, headerRow, activeRow, numberOfColumns) {
var body,
contentAsTxt,
newName,
newDocFileID,
newDoc,
headerValue,
activeCell,
ID = null,
recipient = null,
copyFile,
columnIndex;
for (columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
headerValue = headerRow[columnIndex]
activeCell = activeRow[columnIndex]
activeCell = formatCell(activeCell);
copyBody.replaceText('%' + headerValue + '%', activeCell)
if (headerValue === FILE_NAME_COLUMN_NAME) {
ID = activeCell
} else if (headerValue === EMAIL_COLUMN_NAME) {
recipient = activeCell
}
}
contentAsTxt = copyBody.getText();
newName = PDF_FILE_NAME !== '' ? PDF_FILE_NAME : ID;
Logger.log('newName: ' + newName)
if (RESULTS_FOLDER_ID != '' && newName != '') {
newDocFileID = createNewDocInFolder(newName,RESULTS_FOLDER_ID);// Create the DOC file in the results folder
newDoc = DocumentApp.openById(newDocFileID);
body = newDoc.getBody();
body.setText(contentAsTxt);
newDoc.saveAndClose();
}
}
makeDoc(copyBody, headerRow, row, numberOfColumns);
})
ui.alert('New DOC files created')
return
function formatCell(value) {
var newValue = value;
if (newValue instanceof Date) {
newValue = Utilities.formatDate(
value,
Session.getScriptTimeZone(),
DATE_FORMAT);
} else if (typeof value === 'number') {
newValue = Math.round(value * 100) / 100
}
return newValue;
}
}
var EMAIL_COLUMN_NAME='EMAIL',DATE_FORMAT='dd/MM/yyyy';
函数createNewDocInFolder(新名称、结果\u文件夹\u ID){
var fileID、fileJson、resource;
资源={};
resource.title=newName;
resource.mimeType=mimeType.GOOGLE\u文档;
resource.parents=[{id:RESULTS\u FOLDER\u id}];
fileJson=Drive.Files.insert(资源);
fileID=fileJson.id;//新Google文档的id
返回fileID;
}
函数GeneraAttestati(){
var ui=SpreadsheetApp.getUi();
如果(模板ID==''){
ui.alert('code.gs'中需要定义模板\u ID')
返回
}
var templateFile=DriveApp.getFileById(模板ID)
var activeSheet=SpreadsheetApp.getActiveSheet()
var allRows=activeSheet.getDataRange().getValues()
var headerRow=allRows.shift()
var copyFile=templateFile.makeCopy();
var copyId=copyFile.getId()
var copyDoc=DocumentApp.openById(copyId);
var copyBody=copyDoc.getBody();
var numberOfColumns=headerRow.length;
allRows.forEach(函数(行){
函数makeDoc(copyBody、headerRow、activeRow、numberOfColumns){
var body,
ContentAsText,
新名字,
newDocFileID,
纽多克,
人头价值,
活性细胞,
ID=null,
收件人=空,
复制文件,
柱状指数;
对于(columnIndex=0;columnIndex