Appscript save copy as docx - google-apps-script

hey guys i found this guide https://jeffreyeverhart.com/2020/09/29/auto-fill-a-google-doc-template-from-google-sheet-data/ on transfering data for sheets to google docs but i am trying to get it to save the files as docx since the files will be sent out to customers.
Is there any easy way to get it to work?
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Oppdrettsbevis');
menu.addItem('Generer Oppdrettsbevis', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('xxxxxxxx');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('xxxxxxxxx')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//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[5]) 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]} Employee Details` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{First Name}}', row[0]);
body.replaceText('{{Last Name}}', row[1]);
body.replaceText('{{Position}}', row[2]);
body.replaceText('{{Hire Date}}', friendlyDate);
body.replaceText('{{Hourly Wage}}', row[4]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 6).setValue(url)
})
}

You are asking if it is possible to export a .docx
Try Using, from the crossreferenced post beolw the docToDocx() , which I modified to return the URL. Edited to include the Entire code with some Logger lines.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
//include this function
function docToDocx(id) {
var format = 'docx',
exportLink =
'https://docs.google.com/document/d/' + id + '/export?format=' + format,
blob = UrlFetchApp.fetch(exportLink, {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken(),
},
});
var thisFile = DriveApp.createFile(blob);
return thisFile.getUrl()
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('xx YOUR TEMPLATE ID HERE xx');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('xx YOUR FOLDER ID HERE xx')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//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[5])
{
Logger.log("Document Made already");
Logger.log(row[5]);
return; //Breaks this iteration of the loop should allow next row
}
//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]} Employee Details` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{First Name}}', row[0]);
body.replaceText('{{Last Name}}', row[1]);
body.replaceText('{{Position}}', row[2]);
body.replaceText('{{Hire Date}}', friendlyDate);
body.replaceText('{{Hourly Wage}}', row[4]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
Logger.log("The Google Doc: " + doc.getUrl());
///Call the docToDocx Function and save the Url:
const url = docToDocx( doc.getId() );
Logger.log("The .docX URL: " + url);
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 6).setValue(url);
})
}
After Running the Script I can now see in my Execution log:
Cloud logs
Oct 19, 2022, 12:09:46 PM Info The Google Doc: https://docs.google.com/open?id=xx THIS FILE ID xx
Oct 19, 2022, 12:09:48 PM Info The .docX URL: https://docs.google.com/document/d/xx THIS FILE ID xx_/edit?usp=drivesdk&ouid=xx THIS TOKEN ID XX&rtpof=true&sd=true
Oct 19, 2022, 12:09:50 PM Info The Google Doc: https://docs.google.com/open?id=xx THIS FILE ID xx
Oct 19, 2022, 12:09:52 PM Info The .docX URL: https://docs.google.com/document/d/xx THIS FILE ID xx/edit?usp=drivesdk&ouid=xx THIS TOKEN ID XX&rtpof=true&sd=true
Oct 19, 2022, 12:09:54 PM Info The Google Doc: https://docs.google.com/open?id=xx THIS FILE ID xx
Oct 19, 2022, 12:09:56 PM Info The .docX URL: https://docs.google.com/document/d/xx THIS FILE ID xx/edit?usp=drivesdk&ouid=xx THIS TOKEN ID XX&rtpof=true&sd=true
That shows the Google docs url is different then the supplied uls that will lead to a download of a .docx file. Those docx Urls are also showing in column 5 of the spreadsheet.
The docX function was found at theWizEd's commented crossreference:
Converting a GDoc to docx through GAS produces corrupted document

Related

Google Scripts not skipping rows

Pretty new to using google scripts, so forgive me if I'm doing something silly.
I am trying to write a program to take a large spreadsheet of data and fill an invoice template I have created. I have successfully done this in the past, but now when I tried to apply the app to a new spreadsheet of data, it is no longer functioning properly.
The problem is because the sheet is so large, the scripts times out, then when I go to restart the script, it starts again all the way at the beginning instead of starting where it left off. Originally the function was set to write the URL of the new invoice in the last column of spreadsheet, and the function would skip any rows with entries in that column. This was not happening, so to simplify I added another row that once the invoice is created, the word "DONE" is entered, and then I tried to set it up to skip any row with "DONE" in that column, this still is not working.
Any ideas on how I can get this to work?
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('1yXfcXTESCHqKsfMcgkhYJ9MdVwYoLYPhH1MRv4RyPk0');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1TXEumNJXfgFzPtKLBOAKJBXG-yNnjseQ')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Export Worksheet')
//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[31]=== 'DONE') 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[4]}, ${row[0]} Invoice` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Full Address}}', row[4]);
body.replaceText('{{unit}}', row[5]);
body.replaceText('{{Total}}', row[15]);
body.replaceText('{{Account Num}}', row[2]);
body.replaceText('{{Owner 1}}', row[6]);
body.replaceText('{{Owner 2}}', row[7]);
body.replaceText('{{CO Name}}', row[17]);
body.replaceText('{{St Address}}', row[20]);
body.replaceText('{{Address 1}}', row[18]);
body.replaceText('{{City}}', row[21]);
body.replaceText('{{State}}', row[22]);
body.replaceText('{{CO Zip}}', row[23]);
body.replaceText('{{invoice #}}', row[0]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 30).setValue(url)
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 31).setValue("DONE")
})
}
Try this:
You were checking for DONE in the wrong location
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1yXfc...');
const destinationFolder = DriveApp.getFolderById('1TXE...');
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Export Worksheet');
const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
vs.forEach((r, i) => {
if (r[30] != 'DONE') {
const copy = googleDocTemplate.makeCopy(`${r[4]}, ${r[0]} Invoice`, destinationFolder);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
body.replaceText('{{Full Address}}', r[4]);
body.replaceText('{{unit}}', r[5]);
body.replaceText('{{Total}}', r[15]);
body.replaceText('{{Account Num}}', r[2]);
body.replaceText('{{Owner 1}}', r[6]);
body.replaceText('{{Owner 2}}', r[7]);
body.replaceText('{{CO Name}}', r[17]);
body.replaceText('{{St Address}}', r[20]);
body.replaceText('{{Address 1}}', r[18]);
body.replaceText('{{City}}', r[21]);
body.replaceText('{{State}}', r[22]);
body.replaceText('{{CO Zip}}', r[23]);
body.replaceText('{{invoice #}}', r[0]);
doc.saveAndClose();
const url = doc.getUrl();
sh.getRange(i + 2, 30).setValue(url);
sh.getRange(i + 2, 31).setValue("DONE");
}
});
}

Copy and Share Google Document to new email but suppress notification

I have the below code to copy a template and share it with the same people who has access to the template.
However, I have two issues here:
I do not want them to receive notification each time a copy is made and shared with them
I would also like to share with another person to edit it. The email of the person who can edit is in the google sheet, in column A. This new person should not also receive notificiation.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Create My Checklist');
menu.addItem('New Checklist', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('1qRQ07PDmz1il9IftM9GIJfY37vTusfQZhhNS1BRELJQ');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail()); // Added
//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[10]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy("eResignation Checklist - " + row[2] + " - " + row[4] + " - " + row[5], destinationFolder)
copy.addEditors(emailAddresses);
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Workday ID}}', row[1]);
body.replaceText('{{Full Name}}', row[2]);
body.replaceText('{{Management Level}}', row[3]);
body.replaceText('{{LoS}}', row[4]);
body.replaceText('{{Cost Centre}}', row[5]);
body.replaceText('{{Office Location}}', row[6]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 11).setValue(url)
Drive.Permissions.insert({role: "writer", type: "user", value: row[0]}, copy.getId(), {sendNotificationEmails: false, supportsAllDrives: true});
})}
I believe your goal is as follows.
You want to share the copied file.
The email address for sharing can be retrieved from the column "A".
When the file is shared, you don't want to send a notification email.
In this case, how about the following modification?
Modified script:
In order to use the option of sendNotificationEmails, Drive API is used. So please enable Drive API at Advanced Google services.
From:
sheet.getRange(index + 1, 11).setValue(url)
})}
To:
sheet.getRange(index + 1, 11).setValue(url)
Drive.Permissions.insert({role: "writer", type: "user", value: row[0]}, copy.getId(), {sendNotificationEmails: false}); // Added
})}
Reference:
Permissions: insert
Edit:
From the following replying,
i am really unsure why it says file not found because the copied file is in the shared drive > folder ID: 1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1
I noticed that you are using the shared Drive. And from the following replying,
I have edited the code again as per what I see in my script now. it seems to work partially - for the email in row [0] they are not getting any notification const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail()); how to disable notification for this group?
I noticed that your script was changed from your initial script. In this case, please modify as follows.
From:
sheet.getRange(index + 1, 11).setValue(url)
})}
To:
sheet.getRange(index + 1, 11).setValue(url)
emailAddresses.concat(row[0]).forEach(v =>
Drive.Permissions.insert({role: "writer", type: "user", value: v}, copy.getId(), {sendNotificationEmails: false, supportsAllDrives: true})
);
})}

Creating PDFs from Spreadsheet Need PDF URL to be returned to Spreadsheet

I have a spreadsheet that contains data, a doc that is acting as my template and the start of a script that creates a PDF using the spreadsheet & doc for each line of the data. This works great!
My next step and this is where I am struggling, is I need to put the URL/identifier (from google drive) of each created PDF (each PDF goes to the same folder) into my spreadsheet so that I can then create another script to email the recipient their document.
I've seen this done when using a form and triggers but as I am not actively collecting the data, I cannot figure out this next part.
Thanks,
Nate
/*This function takes the data from the Charitable Tax Receipt summary and passes one row at a time
to the createPDF function.
*/
function createTaxRcptPDFs(){
const DOCTEMPLATE = DriveApp.getFileById("ref");
const TEMPFOLDER = DriveApp.getFolderById("ref1");
const PDFFOLDER = DriveApp.getFolderById("ref2");
const CURRENTSHEET = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TEST");
const DATA = CURRENTSHEET.getRange(5,1,3,20).getDisplayValues(); //change 3 to formula when testing done
// this code block creates a data array to populate each PDF per (row)
DATA.forEach(row => {
const ACTIVECELL = row[20];
createPDF(row[2],row[4],row[5],row[6],row[7],row[1],row[14],row[15],row[16],new Date(),row[1] + "_" + row[2],DOCTEMPLATE,TEMPFOLDER,PDFFOLDER);
});
}
/* This function creates the PDF based on a Doc template and saves it to a specific folder
for future use.
*/
function createPDF(fullName,street,city,state,postalCode,receiptNumber,donation,advantage,eligible,rcptDate,pdfName,DOCTEMPLATE,TEMPFOLDER,PDFFOLDER){
// constants/variables to use
const TEMPFILE = DOCTEMPLATE.makeCopy(TEMPFOLDER);
const TEMPDOCFILE = DocumentApp.openById(TEMPFILE.getId());
// array of values to create body from data
const BODY = TEMPDOCFILE.getBody();
BODY.replaceText("{fullName}", fullName);
BODY.replaceText("{street}", street);
BODY.replaceText("{city}", city);
BODY.replaceText("{state}", state);
BODY.replaceText("{postalCode}", postalCode);
BODY.replaceText("{receiptNumber}", receiptNumber);
BODY.replaceText("{donation}", donation);
BODY.replaceText("{advantage}", advantage);
BODY.replaceText("{eligible}", eligible);
BODY.replaceText("{rcptDate}", rcptDate);
TEMPDOCFILE.saveAndClose();
// create pdf and delete temp file
const PDFBLOB = TEMPFILE.getAs(MimeType.PDF);
PDFFOLDER.createFile(PDFBLOB).setName(pdfName);
TEMPFOLDER.removeFile(TEMPFILE);
}
Solution:
setName(pdfName) returns a File object for chaining, so you can use getDownloadUrl() right after.
var fileUrl = PDFFOLDER.createFile(PDFBLOB).setName(pdfName).getDownloadUrl();
Reference:
getDownloadUrl()

How to export spreadsheet header and last row value to csv file using Google apps script

I'm new here and try to seek some expertise to help to create a google apps script.
I have a spreadsheet and want to export the header and the new added row value to csv file and save it into my local c drive and send an alert email with column B value as a subject.
eg. spreadsheets has 14 columns and I would like to export start from column 2 to csv with values like: "column2 value; column3 value; column4 value; column5 value; column6 value; .....column14 value "
Below is my description workflow :
So everytime when people filled up the value in the google forms and press submit, i will received a new row value in a google sheet. Then i will export the header and the latest row value to csv file into my local c drive and send an alert email with column B value as a subject.
Much appreciated if some expertise can help me on this. Thank you so much. :)
Excel sample.xlsx
See if this helps you:
We'll assume you have the spreadsheet configured as follows:
You've a sheet for submission called Responses
You've a helper sheet needed for the script called Temp which has
the same headers in the first row than the Responses sheet
In the illustrated example below, you want to save as CSV file the headers (Orange) along with the last row submitted (Green)
When you access the script you'll change the following:
The Spreadsheet ID of the whole document
The ID of the tab Temp (numbers found after edit#gid=)
The email address of the recipient
Code:
// Example: https://docs.google.com/spreadsheets/d/1kXaTto1ktqX0XNKQ035897FGX5tV75o7wuYHiNCOqEFfI/edit#gid=1269457815
// In this link the ID of the Spreadsheet is everything after /d/
// which is: 1kXaTto1ktqX0XNKQ035897FGX5tV75o7wuYHiNCOqEFfI
// THE ID of the sheet Temp would be something like: 1269457815
// ---------- Menu ----------
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('CSV File')
.addItem('Step 1: Email CSV', 'EmailRange')
.addItem('Step 2: Save CSV file', 'showurl')
.addToUi();
}
// Choose how do want to export the: csv,pdf,xlsx
EXPORT_TYPE="csv";
function EmailRange() {
// Enter Sheet ID in between ""
var sheetId = "ID GOES HERE";
var ss = SpreadsheetApp.openById(sheetId);
var sheet=ss.getSheetByName("Responses");
// You can set up the headers beforehand
var temp = ss.getSheetByName("Temp");
//Copy range onto Temp sheet
var rangeToCopy = sheet.getRange("A"+(ss.getLastRow())+":N"+(ss.getLastRow()));
// It will erase any previous data
rangeToCopy.copyTo(temp.getRange(2, 1));
// Temporarily hide the sheet
ss.getSheetByName('Responses').hideSheet()
//Authentification
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var url="https://docs.google.com/spreadsheets/d/"+ss.getId()+"/export?format="+EXPORT_TYPE;
//Fetch URL of active spreadsheet
var fetch=UrlFetchApp.fetch(url,params);
//Get content as blob
var blob=fetch.getBlob();
var mimetype;
if(EXPORT_TYPE=="pdf"){
mimetype="application/pdf";
}else if(EXPORT_TYPE=="csv"){
mimetype="text/csv";
}else if(EXPORT_TYPE=="xlsx"){
mimetype="application/xlsx";
}else{
return;
}
// OP: send an alert email with column B value as a subject
var subject = sheet.getRange("B"+(ss.getLastRow()));
var timestamp = sheet.getRange("A"+(ss.getLastRow()));
var Title = subject.getValues();
var signature = timestamp.getValues();
//Change Email Recipient underneath
GmailApp.sendEmail('s.nabil#arrowad.sch.sa',
'Job ID: '+Title,
'Hi there,' + '\n\n' + 'A new entry has been submitted, please find the details in the attached CSV file.' + '\n\n' + 'Submitted on: '+signature,
{
attachments: [{
fileName: Title + "."+EXPORT_TYPE,
content: blob.getBytes(),
mimeType: mimetype
}]
});
//Reshow Response sheet
ss.getSheetByName('Responses').showSheet()
}
function showurl() {
// Enter Spreadsheet ID after d/ and the TAB ID of Temp after gid=
var htmlOutput = HtmlService
.createHtmlOutput('Click here My File to download')
.setWidth(250) //optional
.setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Download CSV File');
}
STEP 1: Sending an email to the defined recipient in the script with the subject being the value of the last row in column B, and the signature the submission date & time, like this:
The CSV file will be attached to the email:
STEP 2: From the CSV File Menu which is created when opening the spreadsheet:
Save file to your Local desktop:

Generating PDF's from form responses with a template.

I'm currently using Form Publisher add in to make PDF's from the form responses with my required template but it is allowing me to generate only 100 files in a month. I have requirement with minimum of 500 files in month and I cannot afford premium license purchase for Form Publisher. Please help me with the basic script to generate PDF's based on form response data with my desired template.
I will share the template and sample sheet if it can be done.
Regards
Gopikrishna
What tools are available for your usage? If you use pdftk, there is a fill_form command that can take a PDF & FDF/XFDF data, and combine the two.
This snippet creates a PDF file using a Google Doc template and the values in a Google Spreadsheet. Just past it into the script editor of the GSheet you are using.
// Replace this with ID of your template document.
var TEMPLATE_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
// var TEMPLATE_ID = '1wtGEp27HNEVwImeh2as7bRNw-tO4HkwPGcAsTrSNTPc' // Demo template
// You can specify a name for the new PDF file here, or leave empty to use the
// name of the template.
var PDF_FILE_NAME = ''
/**
* Eventhandler for spreadsheet opening - add a menu.
*/
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu('Create PDF')
.addItem('Create PDF', '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() {
if (TEMPLATE_ID === '') {
SpreadsheetApp.getUi().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
// Replace the keys with the spreadsheet values
for (;columnIndex < headerRow[0].length; columnIndex++) {
copyBody.replaceText('%' + headerRow[0][columnIndex] + '%',
activeRow[0][columnIndex])
}
// Create the PDF file, rename it if required and delete the doc copy
copyDoc.saveAndClose()
var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'))
if (PDF_FILE_NAME !== '') {
newFile.setName(PDF_FILE_NAME)
}
copyFile.setTrashed(true)
SpreadsheetApp.getUi().alert('New PDF file created in the root of your Google Drive')
} // createPdf()