Last active
September 13, 2018 13:15
-
-
Save nithesh1992/021479fcbd4909f716aa43400342f2f6 to your computer and use it in GitHub Desktop.
Google Apps Scripts (Auto G Doc to PDF conversion and Email Scripts)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Row number from where to fill in the data (starts as 1 = first row) | |
var CUSTOMER_ID = 2; // 1st row may contains column headers | |
// Google Doc id from the document template | |
// (Get ids from the URL) | |
var SOURCE_TEMPLATE = "sdsv"; | |
// In which spreadsheet we have all the customer data | |
var CUSTOMER_SPREADSHEET = "svs"; | |
// In which Google Drive we toss the target documents | |
var TARGET_FOLDER = "wrf"; | |
/** | |
* Duplicates a Google Apps doc | |
* | |
* @return a new document with a given name from the orignal | |
*/ | |
function createDuplicateDocument(sourceId, name) { | |
var source = DriveApp.getFileById(sourceId); | |
var newFile = source.makeCopy(name); | |
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER); | |
targetFolder.addFile(newFile) | |
return DocumentApp.openById(newFile.getId()); | |
} | |
/** | |
* Search a paragraph in the document and replaces it with the generated text | |
*/ | |
function replaceParagraph(doc, keyword, newText) { | |
var ps = doc.getParagraphs(); | |
for(var i=0; i<ps.length; i++) { | |
var p = ps[i]; | |
var text = p.getText(); | |
if(text.indexOf(keyword) >= 0) { | |
p.setText(newText); | |
p.setBold(false); | |
} | |
} | |
} | |
/** | |
* Script entry point | |
*/ | |
function generateCustomerReceipt() { | |
var data = SpreadsheetApp.openById(CUSTOMER_SPREADSHEET); | |
// XXX: Cannot be accessed when run in the script editor? | |
// WHYYYYYYYYY? Asking one number, too complex? | |
//var CUSTOMER_ID = Browser.inputBox("Enter customer number in the spreadsheet", Browser.Buttons.OK_CANCEL); | |
if(!CUSTOMER_ID) { | |
return; | |
} | |
// Fetch variable names | |
// they are column names in the spreadsheet | |
var sheet = data.getSheets()[0];` | |
var values = sheet.getDataRange().getValues(); | |
for (var i = 1; i < values.length; i++) { | |
var customerName = values[i][1] + ' '+ values[i][2]; | |
var amount = values[i][6]; | |
var target = createDuplicateDocument(SOURCE_TEMPLATE, "Receipt_"+customerName); | |
replaceParagraph(target, "{!Name}", customerName); | |
replaceParagraph(target, "{!amount}", "Total : " +amount); | |
Logger.log("Created new document:" + target.getId()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convert2PDF() { | |
var folderId="1ds"; // Source Folder where all google docs are available | |
var docFolder = DriveApp.getFolderById(folderId); | |
Logger.log(docFolder.getName()); | |
var files = docFolder.getFiles(); | |
while (files.hasNext()) { | |
var file = files.next(); | |
Logger.log(file.getName()); | |
var docblob =file.getAs('application/pdf'); | |
docblob.setName(file.getName() + ".pdf"); | |
var pdfFile = DriveApp.createFile(docblob); | |
//destFolder.addFile(pdfFile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment