As the title says, I have data (Q & A grouped by subject) that I have to export to a document file.
As shown above, the source data on the left should be formatted like the example on the right
I'm looking for a free solution. Google helped me find this which looks deprecated according to comments. So I'll be very glad if at least you put me on the right path to realise this task given that I have some basic JS experience (but I've never scripted in GS).
Edit.1:
I started indeed learning how to script with Google Apps since it's a time saver as long as I'm using Google for almost everything.
At the end I'll post the full code here to help any beginner else like me.
For now I'm stuck at these points:
-
I'm adding a title, then I'm trying to customize it's style (font, color, alignment), but it doesn't work as expected (not everything). Plus, it looks like that order matters :-/
The following snippet gives the output in the picture:
// Define a style var titleStyle1 = {}; titleStyle1[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.COMIC_SANS_MS; titleStyle1[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ff0000'; // Red titleStyle1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; titleStyle1[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.TITLE; var titleStyle2 = {}; titleStyle2[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.TITLE; titleStyle2[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.COMIC_SANS_MS; titleStyle2[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ff0000'; // Red titleStyle2[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; var titleStyle3 = {}; titleStyle3[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.TITLE; titleStyle3[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; titleStyle3[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.COMIC_SANS_MS; titleStyle3[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ff0000'; // Red var titleStyle4 = {}; titleStyle4[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.COMIC_SANS_MS; titleStyle4[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ff0000'; // Red titleStyle4[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; //titleStyle4[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.TITLE; // Create and open a new document. var doc = DocumentApp.create(outputDocDesiredName); var body = doc.getBody(); body.setAttributes(docStyle) // Append a document header paragraph. body.appendParagraph('Test title').setAttributes(titleStyle1); body.appendParagraph('Test title').setAttributes(titleStyle2); body.appendParagraph('Test title').setAttributes(titleStyle3); body.appendParagraph('Test title').setAttributes(titleStyle4);
Notice that in the last case, only when DocumentApp.Attribute.HEADING
isn't set that the other styles are taken into account. I wonder why.
-
Instead of setting the font
COMIC_SANS_MS
every time for every added text, is there a way to set a default font (among other attributes) for the newly created document?The following code does change nothing:
var docStyle = {}; docStyle[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.COMIC_SANS_MS; var body = doc.getBody(); body.setAttributes(docStyle);
Best Answer
There are several add-ons for Google Documents and Sheets that already do that. One of them is Autocrat.
Some of these add-ons work on "merge" model: a template and data source.
The template holds the formatting, fixed text and placeholders for variable text. The spreadsheet is used as the data source, sometimes it has the menu options to trigger the merge function.
References
Overview of add-ons - Docs editors Help