Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('XYZ Menu')
.addItem('Generate PO', 'createDcument')
.addToUi();
}
function createDcument() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var activeRange = activeSheet.getActiveRange();
var row_data = activeRange.getDisplayValues();
var scriptProperties = PropertiesService.getScriptProperties();
var template_id = scriptProperties.getProperty('template_id'); // document template
/***Modify indexs if you the spread sheet columns shuffle **/
var attributes = {
bill_no: "0",
vendor: "1" ,
date: "2" ,
description: "3" ,
length: "4" ,
width: "5",
height: "6",
weight: "7"
}
/****************/
var documentId = DriveApp.getFileById(template_id).makeCopy().getId();
DriveApp.getFileById(documentId).setName('Purchase Order PO# '+ row_data[0][3]);
var body = DocumentApp.openById(documentId).getBody();
for (var prop in attributes) {
var index = attributes[prop];
var propValue = row_data[0][index];
propValue = propValue ? propValue : "";
body.replaceText('##'+prop+'##', propValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.