Add plain text replacement from spreadsheet contents

This commit is contained in:
Adam Goldsmith 2020-03-17 16:35:47 -04:00
parent d5d0aae48b
commit 61ff5a5cc7

19
Code.ts
View File

@ -44,16 +44,33 @@ function copyBody(
} }
function generateForCurrentRow() { function generateForCurrentRow() {
const cell = SpreadsheetApp.getActive().getCurrentCell();
const template = DocumentApp.openById( const template = DocumentApp.openById(
'1V0uMuM80BGpjpdt1AmuzlU97tDI_u-y2rOfdl4tkqmc' '1V0uMuM80BGpjpdt1AmuzlU97tDI_u-y2rOfdl4tkqmc'
); );
const spreadsheet = SpreadsheetApp.getActive();
const cell = spreadsheet.getCurrentCell();
if (!cell) throw new Error('No Cell selected for operation on row');
const source_doc = DocumentApp.openById( const source_doc = DocumentApp.openById(
'1tB9--ilbfDixuQAEMk0_D6KqNFToQzs-au6NiotCH5M' '1tB9--ilbfDixuQAEMk0_D6KqNFToQzs-au6NiotCH5M'
); );
const out_doc = template; // TODO: make a copy of template, and write to that const out_doc = template; // TODO: make a copy of template, and write to that
// do text replacement
// TODO: could be more efficient
const values = spreadsheet
.getActiveSheet()
.getDataRange()
.getValues();
const headers = values[0];
const row = values[cell.getRow() - 1];
headers.forEach((header, index) => {
out_doc.getBody().replaceText(`{{${header}}}`, String(row[index]));
out_doc.getHeader().replaceText(`{{${header}}}`, String(row[index]));
out_doc.getFooter().replaceText(`{{${header}}}`, String(row[index]));
});
const insert_point = out_doc const insert_point = out_doc
.getBody() .getBody()
.findText('{{body}}') .findText('{{body}}')