Skip to content

Instantly share code, notes, and snippets.

@barrieroberts
Last active April 5, 2021 22:48
Show Gist options
  • Save barrieroberts/6bcf64af3e7acf1aae5db720f3905d8e to your computer and use it in GitHub Desktop.
Save barrieroberts/6bcf64af3e7acf1aae5db720f3905d8e to your computer and use it in GitHub Desktop.
Make sales quotes
//CHAPTER 3 - Bound to a Google Sheet
function makeSalesQuotes() {
//Get quote data, master, folder & timezone
const ss = SpreadsheetApp.getActiveSpreadsheet(),
sh = ss.getSheetByName('QUOTES'),
quoteMaster = DriveApp.getFileById('DOCUMENT ID'),
fQuotes = DriveApp.getFolderById('FOLDER ID'),
timeZone = Session.getScriptTimeZone(),
data = sh.getDataRange().getValues();
data.splice(0, 2);
//Loop through quotes and make those ticked
data.forEach(function (row, r) {
if (row[16] === true) {
//Set up variables for quote data
var quoteNo = row[0], compName = row[1], compAdd1 = row[2],
compAdd2 = row[3], custName = row[4], custAdd1 = row[5],
custAdd2 = row[6], prepared = row[7], expiry = row[8],
delivery = row[9], tcLink = row[10], item = row[11],
qty = row[12], price = row[13], total = row[14];
//Format data
var quoteNo = ('0000' + quoteNo).slice(-4);
var price = price.toFixed(2);
var total = total.toFixed(2);
var prepared = Utilities.formatDate(prepared, timeZone, 'dd-MM-yyyy');
var expiry = Utilities.formatDate(expiry, timeZone, 'dd-MM-yyyy');
//Create quote document and get header and body
var newQuote = quoteMaster.makeCopy("Quote #" + quoteNo, fQuotes);
var newQuoteDoc = DocumentApp.openById(newQuote.getId());
var header = newQuoteDoc.getHeader();
var body = newQuoteDoc.getBody();
//Replace placeholders in header
header.replaceText('{{COMPANY NAME}}', compName);
header.replaceText('{{ADDRESS1}}', compAdd1);
header.replaceText('{{ADDRESS2}}', compAdd2);
//Replace placeholders in body
const placeholders = ['{{QUOTENO}}', '{{CUSTOMER NAME}}',
'{{CUSTOMER ADDRESS1}}', '{{CUSTOMER ADDRESS2}}',
'{{PREPARED DATE}}', '{{EXPIRY DATE}}',
'{{ITEM}}', '{{QTY}}', '{{PRICE}}', '{{TOTAL}}',
'{{DELIVERY DAYS}}',
'{{COMPANY NAME}}'];
var quoteData = [quoteNo, custName, custAdd1, custAdd2,
prepared, expiry, item, qty, price, total, delivery,
compName];
placeholders.forEach(function (ph, p) {
body.replaceText(ph, quoteData[p]);
});
//Add link
var link = newQuoteDoc.getBody().findText("{{TandC}}")
.getElement().asText();
link.setLinkUrl(tcLink);
body.replaceText("{{TandC}}", "Terms and conditions document");
newQuoteDoc.saveAndClose();
//Add quote link to sheet and reset checkbox
sh.getRange(r + 3, 16)
.setFormula('=HYPERLINK("' + newQuote.getUrl() + '","Link")');
sh.getRange(r + 3, 17).setValue("FALSE");
}
});
}
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu("QUOTE")
.addItem("Make sales quotes", 'makeSalesQuotes')
.addToUi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment