1

Can anyone tell me why this code is returning an error that says, "invalid color?"

It seems to be referring to the BACKGROUND_COLOR attribute. But I can't figure out why.

function myFunction() {
  var bibiDoc = {};
  bibiDoc[DocumentApp.Attribute.BACKGROUND_COLOR] = 0x000000;
  bibiDoc[DocumentApp.Attribute.FONT_FAMILY] = 'Courier New';
  bibiDoc[DocumentApp.Attribute.FOREGROUND_COLOR] = 0x00FF00;
  bibiDoc[DocumentApp.Attribute.FONT_SIZE] = 12;


  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var par = body.appendParagraph("test");

  par.setAttributes(bibiDoc);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
GMath314
  • 73
  • 1
  • 1
  • 7
  • It's not returning INVALID COLOR. I tested your code and no errors concerning BACKGROUND COLOR. http://imgur.com/LxBMvIZ Try copying the color codes into a new script project to see if that's really the cause of errors. – ReyAnthonyRenacia Oct 23 '16 at 23:09
  • Try to use a string of instead of an hexadecimal number. (reference: https://developers.google.com/apps-script/reference/document/attribute) – Rubén Oct 23 '16 at 23:35

1 Answers1

1

You can use the Hex Code format but don't forget to put it inside quotation marks:

 // Define a style with yellow background.
 var highlightStyle = {};
 highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFF00';
 highlightStyle[DocumentApp.Attribute.BOLD] = true;

 // Insert "Hello", highlighted.
 DocumentApp.getActiveDocument().editAsText()
   .insertText(0, 'Hello\n')
   .setAttributes(0, 4, highlightStyle);

And a here's a list of Hex Codes you can try.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56