Skip to content

Instantly share code, notes, and snippets.

@raghuvarmabh
Created August 21, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raghuvarmabh/76ee43f848c77f67d340a25094e6ede5 to your computer and use it in GitHub Desktop.
Save raghuvarmabh/76ee43f848c77f67d340a25094e6ede5 to your computer and use it in GitHub Desktop.
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