Add function/menu item to generate document for each row in the sheet

This commit is contained in:
Adam Goldsmith 2020-03-18 16:30:18 -04:00
parent 2b377bbfca
commit 76b4f2a73c

37
Code.ts
View File

@ -2,6 +2,7 @@ function onOpen() {
const ui = SpreadsheetApp.getUi(); const ui = SpreadsheetApp.getUi();
ui.createMenu('CMS Document Generation') ui.createMenu('CMS Document Generation')
.addItem('Generate document for current row', 'generateForCurrentRow') .addItem('Generate document for current row', 'generateForCurrentRow')
.addItem('Generate document for all rows', 'generateForAllRows')
.addToUi(); .addToUi();
} }
@ -71,15 +72,11 @@ function trashFiles(files: GoogleAppsScript.Drive.FileIterator) {
} }
} }
function generateForCurrentRow() { function generateForRow(
const spreadsheet = SpreadsheetApp.getActive(); spreadsheet: GoogleAppsScript.Spreadsheet.Spreadsheet,
const cell = spreadsheet.getCurrentCell(); row_num: number
if (!cell) throw new Error('No Cell selected for operation on row'); ) {
const row = spreadsheetRowToObject(spreadsheet.getActiveSheet(), row_num);
const row = spreadsheetRowToObject(
spreadsheet.getActiveSheet(),
cell.getRow()
);
const template_doc = DocumentApp.openById(row['Template ID']); const template_doc = DocumentApp.openById(row['Template ID']);
@ -143,3 +140,25 @@ function generateForCurrentRow() {
// create PDF file // create PDF file
out_folder.createFile(out_file.getAs('application/pdf')); out_folder.createFile(out_file.getAs('application/pdf'));
} }
function generateForCurrentRow() {
const spreadsheet = SpreadsheetApp.getActive();
const cell = spreadsheet.getCurrentCell();
if (!cell) throw new Error('No Cell selected for operation on row');
generateForRow(spreadsheet, cell.getRow());
}
function generateForAllRows() {
const spreadsheet = SpreadsheetApp.getActive();
const row_count = spreadsheet
.getActiveSheet()
.getDataRange()
.getNumRows();
for (let row_num = 2; row_num < row_count; row_num++) {
generateForRow(spreadsheet, row_num);
}
}