Google Apps Script - merge doc - multi page with dynamic tables on each page

36 views
Skip to first unread message

urwa shabir

unread,
May 29, 2019, 3:00:46 PM5/29/19
to Google Apps Script Community
Hi,
I am working on an invoice generator that usually takes 2 to 3 pages. This is the current logic which is working for the most part.

- Take data from google sheet (variable).
- Determine total rows.
- Divide total rows into chunks (15 rows for page 1 due to initial info etc, 25 (or remaining) for page 2 and onwards).
- To comply with the invoice formatting I do the following, lets say we have 35 data rows for a particular invoice
   -- For page 1 append the following [header row + 15 data rows + subtotal] and insert table on page 1.
   -- Page break
   -- For page 2 append the following [header + 35 - 15 subtotal + data rows + tax deduction + total] and insert table on page 2.

Now the problem occurs when for some rows the data is on multiple line. This causes table on page 1 to move to second page and then there is a page break and table 2 moves to page 3.

Is there something I am completely missing in inserting dynamic number of tables with dynamic number of rows (some being multiline) in a google doc. If you think another way, the problem is at what point to break the data array to insert subTotal row so that table stays on a single page in google doc.

I came up with one other logic (but I could not implement it since google docs as no method to determine if we have reached the end of a page).

- Insert empty table on page 1.
- Keep appending data rows until the end of page or one less row than end of page.
- Sum all costs and append subtotal row.
- Add page break.
- Insert empty table on page 2.
- Append subtotal.
- keep appending data rows until the end of page or one less row than end of page.
- Sum all costs and append subtotal row.
- Add page break.
- Repeat until last data row is reached.
- In which case add tax deduction and total row.

But as mentioned, I coulld not find any method that could tell if I am at the end of the page or track page count in any way.


If any one can help in this regard that would be great.

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages