Can you add a Horizontal Line to a Google Document using their API?

I’m working on a PHP project where I need to add different elements to a Google Doc. I can add text and page breaks, but I’m stuck on horizontal lines. Here’s what I’ve tried:

$textRequest = new MyGoogleDocsRequest([
    'addContent' => [
        'content' => 'Some text here',
        'position' => $insertSpot,
    ],
]);

$pageBreakRequest = new MyGoogleDocsRequest([
    'addPageBreak' => [
        'position' => $insertSpot,
    ],
]);

$updateDoc = new MyGoogleDocsBatchUpdate(['changes' => [$textRequest, $pageBreakRequest]]);
$googleDocsAPI->updateDocument($docId, $updateDoc);

This works fine for text and page breaks. But I can’t figure out how to add a horizontal line. The manual Google Docs interface lets you do it, and you can see horizontal lines when you check a document’s structure. But I can’t find any API method for it.

Does anyone know if it’s possible to add a horizontal line with the Google Docs API? If so, how? I’m trying to copy one Google Doc into another, so if there’s a better way to do that, I’d love to hear it too!

I’ve been in a similar situation, and I found a workaround that might help. Instead of a traditional horizontal line, you can create the illusion of one using text with special Unicode characters. Here’s what I did:

$requests = new Google_Service_Docs_Request([
‘insertText’ => [
‘text’ => str_repeat(‘─’, 50) . ‘\n’,
‘location’ => [‘index’ => $endIndex]
]
]);

$requests = new Google_Service_Docs_Request([
‘updateTextStyle’ => [
‘range’ => [
‘startIndex’ => $endIndex,
‘endIndex’ => $endIndex + 51
],
‘textStyle’ => [
‘fontSize’ => [‘magnitude’ => 6, ‘unit’ => ‘PT’]
],
‘fields’ => ‘fontSize’
]
]);

This creates a line of small dashes that spans the width of the document. It’s not perfect, but it’s a decent alternative when you need something that looks like a horizontal line. You can adjust the number of repetitions and font size to fit your needs. Just remember that this method might not scale well if the document width changes.

hey sophia, i’ve dealt with this before. sadly, there’s no direct way to add horizontal lines via the API. :frowning: but here’s a workaround - you can use a paragraph with a bottom border. it looks like this:

$requests[] = new Google_Service_Docs_Request([
    'insertText' => [
        'text' => "\n",
        'location' => ['index' => $endIndex]
    ]
]);
$requests[] = new Google_Service_Docs_Request([
    'updateParagraphStyle' => [
        'range' => [
            'startIndex' => $endIndex,
            'endIndex' => $endIndex + 1
        ],
        'paragraphStyle' => [
            'borderBottom' => [
                'color' => ['color' => ['rgbColor' => ['red' => 0, 'green' => 0, 'blue' => 0]]],
                'width' => ['magnitude' => 1, 'unit' => 'PT'],
                'dashStyle' => 'SOLID'
            ]
        ],
        'fields' => 'borderBottom'
    ]
]);

hope this helps!

While the Google Docs API doesn’t offer a direct method for inserting horizontal lines, there’s an alternative approach you might find useful. Instead of a true horizontal line, you can create a single-cell table with a border, which visually resembles a horizontal line. Here’s how you could implement this:

$requests[] = new Google_Service_Docs_Request([
    'insertTable' => [
        'rows' => 1,
        'columns' => 1,
        'location' => ['index' => $endIndex]
    ]
]);

$requests[] = new Google_Service_Docs_Request([
    'updateTableCellStyle' => [
        'tableStartLocation' => ['index' => $endIndex],
        'tableCellStyle' => [
            'borderBottom' => [
                'color' => ['color' => ['rgbColor' => ['red' => 0, 'green' => 0, 'blue' => 0]]],
                'width' => ['magnitude' => 1, 'unit' => 'PT'],
                'dashStyle' => 'SOLID'
            ]
        ],
        'fields' => 'borderBottom'
    ]
]);

This method creates a subtle line that integrates well with the document’s layout. It’s not perfect, but it’s a practical workaround given the API’s limitations.