Search code examples
google-apps-scriptgoogle-docs

How do I insert a Horizontal line in a google document via Google Apps Script?


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.


Solution

  • 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()