Format a Google Documents after a template, using Apps Script

138 views Asked by At

Background: Company is updating the style and I've made an addon that should update existing documents to the current style-profile.

Solution: A Google App Script [add-on] reads the right formating from a template-document and updates the active document to mirror it.

Snippet: This don't work correctly

var deltaAttri = [
    "BACKGROUND_COLOR",
    "FOREGROUND_COLOR",
    "FONT_FAMILY",
    "FONT_SIZE",
    "SPACING_AFTER",
    "SPACING_BEFORE"
];
var baseAttr = heading1.getAttributes();
var templateAttri = templateHeading1.getAttributes();
for(var d = 0;d<deltaAttri.length;d++){
  baseAttr[deltaAttri[d]] = templateAttri [deltaAttri[d]];
}
heading1.setAttributes(baseAttr);

Problem: Almost every attribute i fetch ( heading1.getAttributes() ) is null.

Heading1={
  FONT_SIZE=null, 
  SPACING_BEFORE=null, 
  SPACING_AFTER=null, 
  FOREGROUND_COLOR=#073763, 
  FONT_FAMILY=null, 
  BACKGROUND_COLOR=null
}

Am I doing this wrong or is this outside the scipe of what to use App Script for?

I initially thought null ment the default value was set. If so, can I access/change the default?

1

There are 1 answers

1
Cooper On

Here's something I did with an Envelope Printer recently. It's a fairly standard way of loading an object.

function setupDocument() 
{
  var doc=DocumentApp.getActiveDocument();
  var envelope10={};
  envelope10[DocumentApp.Attribute.PAGE_HEIGHT]=296;
  envelope10[DocumentApp.Attribute.PAGE_WIDTH]=684;
  envelope10[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  envelope10[DocumentApp.Attribute.FONT_SIZE] = 14;
  envelope10[DocumentApp.Attribute.BOLD] = true;
  envelope10[DocumentApp.Attribute.LINE_SPACING]=1;
  doc.getBody().clear().setAttributes(envelope10);
}