Create real output document and PDF
This commit is contained in:
parent
797a9c1623
commit
ebf76d2653
57
Code.ts
57
Code.ts
@ -43,32 +43,49 @@ function copyBody(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function spreadsheetRowToObject(
|
||||||
|
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
||||||
|
rownum: number
|
||||||
|
): { [key: string]: any } {
|
||||||
|
// TODO: could be more efficient
|
||||||
|
const values = sheet.getDataRange().getValues();
|
||||||
|
const headers = values[0];
|
||||||
|
const row_data = values[rownum - 1];
|
||||||
|
|
||||||
|
return headers.reduce((acc, header, index) => {
|
||||||
|
acc[String(header)] = row_data[index];
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
function generateForCurrentRow() {
|
function generateForCurrentRow() {
|
||||||
const template = DocumentApp.openById(
|
|
||||||
'1V0uMuM80BGpjpdt1AmuzlU97tDI_u-y2rOfdl4tkqmc'
|
|
||||||
);
|
|
||||||
const spreadsheet = SpreadsheetApp.getActive();
|
const spreadsheet = SpreadsheetApp.getActive();
|
||||||
const cell = spreadsheet.getCurrentCell();
|
const cell = spreadsheet.getCurrentCell();
|
||||||
if (!cell) throw new Error('No Cell selected for operation on row');
|
if (!cell) throw new Error('No Cell selected for operation on row');
|
||||||
|
|
||||||
const source_doc = DocumentApp.openById(
|
const row = spreadsheetRowToObject(
|
||||||
'1tB9--ilbfDixuQAEMk0_D6KqNFToQzs-au6NiotCH5M'
|
spreadsheet.getActiveSheet(),
|
||||||
|
cell.getRow()
|
||||||
);
|
);
|
||||||
const out_doc = template; // TODO: make a copy of template, and write to that
|
|
||||||
|
const source_doc = DocumentApp.openById(row['Document ID']);
|
||||||
|
|
||||||
|
const template_file = DriveApp.getFileById(row['Template ID']);
|
||||||
|
const out_folder = DriveApp.getFolderById(
|
||||||
|
'1ROyJXk-QANTHM6Jiw0ne3EQiR1f2UsLr'
|
||||||
|
);
|
||||||
|
|
||||||
|
const out_file = template_file.makeCopy(
|
||||||
|
row['Document Name'] + row['Version'],
|
||||||
|
out_folder
|
||||||
|
);
|
||||||
|
const out_doc = DocumentApp.openById(out_file.getId());
|
||||||
|
|
||||||
// do text replacement
|
// do text replacement
|
||||||
// TODO: could be more efficient
|
Object.entries(row).forEach(([header, data]) => {
|
||||||
const values = spreadsheet
|
out_doc.getBody().replaceText(`{{${header}}}`, String(data));
|
||||||
.getActiveSheet()
|
out_doc.getHeader().replaceText(`{{${header}}}`, String(data));
|
||||||
.getDataRange()
|
out_doc.getFooter().replaceText(`{{${header}}}`, String(data));
|
||||||
.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
|
||||||
@ -90,4 +107,8 @@ function generateForCurrentRow() {
|
|||||||
// insert with index 0 is an append, for some reason
|
// insert with index 0 is an append, for some reason
|
||||||
copyBody(source_doc.getBody(), out_doc.getBody(), index + 1);
|
copyBody(source_doc.getBody(), out_doc.getBody(), index + 1);
|
||||||
out_doc.getBody().removeChild(parent);
|
out_doc.getBody().removeChild(parent);
|
||||||
|
out_doc.saveAndClose();
|
||||||
|
|
||||||
|
// create PDF file
|
||||||
|
out_folder.createFile(out_file.getAs('application/pdf'));
|
||||||
}
|
}
|
||||||
|
9
tsconfig.json
Normal file
9
tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es5",
|
||||||
|
"lib": ["es2017"],
|
||||||
|
"strict": true,
|
||||||
|
"module": "es2015",
|
||||||
|
"moduleResolution": "node"
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user