3

I wrote a script that takes some data from a google spreadsheet and writes it in several separate tables in a google document. Since I need to physically cut these tables from the printed document, I'd like to add horizontal lines between them. I guess I can insert an image of a line, via

var cutline = UrlFetchApp.fetch("SOME URL");
body.appendInlineImage(resp.getBlob());

where "SOME URL" is the url for the image.

Another way I can think of would be creating a one-column table and add my tables in its cells. The horizontal lines for the table would be my cutting guides.

However, from the user interface I a horizontal line can be easily added via "Insert>Horizontal Line". I was not able to do find a way to do that from Google Apps Script.

Is this unsupported?

Can someone confirm that and/or possibly suggest method that is less cumbersome than those I thought of?

EDIT: I have also found the HorizontalRule class, which I do not understand entirely. However I don't think it's what I'm looking for. Another alternative could be an InlineDrawing. However, from this guide, I gather that an InlineDrawing can only be manipulated, but not added programmatically.

PFB
  • 177
  • 2
  • 13
  • Possible duplicate of [Add images to Google Document via Google Apps Script](https://stackoverflow.com/questions/7898497/add-images-to-google-document-via-google-apps-script) – Rubén Feb 02 '18 at 15:08
  • 1
    Hi Ruben, please read my question. I'm not asking how to *insert* an image in a document. In fact, I'm proposing that as a possible alternative to what I'm looking for. I'm asking whether it's possible to **insert a line** as one does from the menu in the user interface (or possible alternative, if that's not possible). – PFB Feb 02 '18 at 15:17

2 Answers2

4

HorizontalRule should do the magic. This code worked for me:

DocumentApp.getActiveDocument().getBody().appendHorizontalRule();

More info: https://developers.google.com/apps-script/reference/document/horizontal-rule#copy()

Kyrill
  • 356
  • 2
  • 6
  • Thanks, that worked indeed! But I'm still a bit confused by the documentation. Apparently this class has also a setAttributes method. However the example is exactly the same as in paragraph, and gives no clue as to what the parameters might be. This was one of the things that confused me about HorizontalRule. I also tried placing a HorizontalRule and getting its attributes, but all I get is an empty array. Anyways, thanks, I now have my horizontal lines all right! – PFB Feb 03 '18 at 07:16
  • it seems that very few attributes can be set to HorizontalRule, even manually using the doc interface. You can get these attributes (line spacing or background color for example) with script but all my attempts to set them have failed... – Serge insas Feb 03 '18 at 08:23
0
DocumentApp.getActiveDocument()..getBody().insertHorizontalRule(0)
  • 1
    What's the benefit of `insertHorizontalRule()` over `appendHorizontalRule()`, as used in the accepted answer from three years ago? – Jeremy Caney Dec 20 '21 at 03:04
  • You get to specify where to put the horizontal line by putting the child index number in the parameters – Josh Bullough Dec 21 '21 at 02:05