Created
January 27, 2016 22:52
-
-
Save DevPicon/a0f64078a2a6e7418eb4 to your computer and use it in GitHub Desktop.
This file contains 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
var CUSTOMER_ID = 2; | |
var SOURCE_TEMPLATE = "1YsSNl__rJon39zDyEnrmfJezc7JdAtAPPCPWj9iZGXo"; | |
var CUSTOMER_SPREADSHEET = "1X7WDMsqHotSTnobp57wiw4_I7Zcn6RdvOhBVpUX85-M"; | |
var TARGET_FOLDER = "0ByGfFMPl-WggaGw5ejFFTGgxajg"; | |
function getRowAsArray(sheet, row, numRows) { | |
var dataRange = sheet.getRange(row, 1, numRows, 99); | |
var data = dataRange.getValues(); | |
var columns = []; | |
for (i in data) { | |
var row = data[i]; | |
Logger.log("Got row", row); | |
for(var l=0; l<99; l++) { | |
var col = row[l]; | |
// First empty column interrupts | |
if(!col) { | |
break; | |
} | |
columns.push(col); | |
} | |
} | |
return columns; | |
} | |
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()); | |
} | |
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); | |
} | |
} | |
} | |
function generateCustomerContract() { | |
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 columns = getRowAsArray(sheet, 1, 1); | |
Logger.log("Processing columns:" + columns); | |
var customerData = getRowAsArray(sheet, CUSTOMER_ID, 4); | |
Logger.log("Processing data:" + customerData); | |
Logger.log("Processing data:" + customerData.length); | |
for(var z=0; z<customerData.length; z++){ | |
Logger.log("Entro " + z); | |
var attendantName = customerData[z]; | |
Logger.log("Name data:" + attendantName); | |
var target = createDuplicateDocument(SOURCE_TEMPLATE, attendantName + " certificado"); | |
Logger.log("Created new document:" + target.getId()); | |
for(var i=0; i<columns.length; i++) { | |
var key = columns[i]; | |
Logger.log("Key:" + key); | |
// We don't replace the whole text, but leave the template text as a label | |
var text = customerData[i] || ""; // No Javascript undefined | |
Logger.log("Text:" + text); | |
var value = key + " " + text; | |
Logger.log("Value:" + value); | |
replaceParagraph(target, key, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment