Use new header names, get document link without needing ID column
This commit is contained in:
parent
c2cb5dca20
commit
5c6b6fda02
51
Code.ts
51
Code.ts
@ -4,6 +4,20 @@ const DOCUMENT_TEMPLATE_IDS = {
|
||||
|
||||
const OUTPUT_FOLDER_ID = '1ROyJXk-QANTHM6Jiw0ne3EQiR1f2UsLr';
|
||||
|
||||
declare namespace GoogleAppsScript {
|
||||
namespace Spreadsheet {
|
||||
interface RichTextValue {
|
||||
getLinkUrl(): string | null;
|
||||
getLinkUrl(startOffset: number, endOffset: number): string | null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type DocSection =
|
||||
| GoogleAppsScript.Document.Body
|
||||
| GoogleAppsScript.Document.HeaderSection
|
||||
| GoogleAppsScript.Document.FooterSection;
|
||||
|
||||
function onOpen() {
|
||||
const ui = SpreadsheetApp.getUi();
|
||||
ui.createMenu('CMS Document Generation')
|
||||
@ -13,11 +27,6 @@ function onOpen() {
|
||||
.addToUi();
|
||||
}
|
||||
|
||||
type DocSection =
|
||||
| GoogleAppsScript.Document.Body
|
||||
| GoogleAppsScript.Document.HeaderSection
|
||||
| GoogleAppsScript.Document.FooterSection;
|
||||
|
||||
function findAllFiles(path: string, folder: GoogleAppsScript.Drive.Folder) {
|
||||
const out: string[][] = [];
|
||||
|
||||
@ -96,11 +105,11 @@ function copySection(source: DocSection, dest: DocSection, index?: number) {
|
||||
function spreadsheetRowToObject(
|
||||
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
||||
rownum: number
|
||||
): { [key: string]: any } {
|
||||
): { [key: string]: GoogleAppsScript.Spreadsheet.RichTextValue } {
|
||||
// TODO: could be more efficient
|
||||
const values = sheet.getDataRange().getValues();
|
||||
const headers = values[0];
|
||||
const row_data = values[rownum - 1];
|
||||
const range = sheet.getDataRange();
|
||||
const headers = range.getValues()[0];
|
||||
const row_data = range.getRichTextValues()[rownum - 1];
|
||||
|
||||
return headers.reduce((acc, header, index) => {
|
||||
acc[String(header)] = row_data[index];
|
||||
@ -120,19 +129,26 @@ function generateForRow(
|
||||
) {
|
||||
const row = spreadsheetRowToObject(spreadsheet.getActiveSheet(), row_num);
|
||||
|
||||
if (!(row['Document Type'] in DOCUMENT_TEMPLATE_IDS))
|
||||
throw new Error(`${row['Document Type']} is not a valid type of document!`);
|
||||
if (!(row['Type'].getText() in DOCUMENT_TEMPLATE_IDS))
|
||||
throw new Error(`${row['Type']} is not a valid type of document!`);
|
||||
|
||||
const template_doc = DocumentApp.openById(
|
||||
DOCUMENT_TEMPLATE_IDS[
|
||||
row['Document Type'] as keyof typeof DOCUMENT_TEMPLATE_IDS
|
||||
row['Type'].getText() as keyof typeof DOCUMENT_TEMPLATE_IDS
|
||||
]
|
||||
);
|
||||
|
||||
const source_file = DriveApp.getFileById(row['Document ID']);
|
||||
const link = row['Document'].getLinkUrl();
|
||||
if (link === null) {
|
||||
throw new Error(`Link missing for ${row['Document'].getText()}`);
|
||||
}
|
||||
// TODO: should probably handle this better
|
||||
if (!link.includes('document')) return;
|
||||
|
||||
const source_file = DriveApp.getFileById(DocumentApp.openByUrl(link).getId());
|
||||
const out_folder = DriveApp.getFolderById(OUTPUT_FOLDER_ID);
|
||||
|
||||
const out_name = row['Document Name'] + '_' + row['Version'];
|
||||
const out_name = row['Document'] + '_' + row['Version'];
|
||||
|
||||
// Delete old files with the same name
|
||||
trashFiles(out_folder.getFilesByName(out_name));
|
||||
@ -177,9 +193,10 @@ function generateForRow(
|
||||
|
||||
// do text replacement
|
||||
Object.entries(row).forEach(([header, data]) => {
|
||||
out_doc.getBody().replaceText(`{{${header}}}`, String(data));
|
||||
out_doc.getHeader().replaceText(`{{${header}}}`, String(data));
|
||||
out_doc.getFooter().replaceText(`{{${header}}}`, String(data));
|
||||
let replacement = data.getText();
|
||||
out_doc.getBody().replaceText(`{{${header}}}`, replacement);
|
||||
out_doc.getHeader().replaceText(`{{${header}}}`, replacement);
|
||||
out_doc.getFooter().replaceText(`{{${header}}}`, replacement);
|
||||
});
|
||||
|
||||
out_doc.saveAndClose();
|
||||
|
Reference in New Issue
Block a user