Search code examples
google-apps-scriptgoogle-apigoogle-docsgoogle-docs-api

Set table borders in Google Document with Google Script App


I am trying to alter table borders with Google Script App (and Google API). I based my code on this post here, but without success.

I am getting these two errors:

Cannot read properties of undefined (reading 'body') Invalid requests[0].updateTableCellStyle: The provided table start location is invalid.

Could you help me?

function susbstituirTabela (docID, arrayTabela, texttofind){
    const doc = DocumentApp.openById(docID);
    var body = doc.getBody();
    var rgel = body.findText(texttofind);
    var element = rgel.getElement();
    var childIndex = body.getChildIndex(element.getParent());
    var arrayFinal = [];
    for (let i = 0; i < arrayTabela.length; i++) {
      let t = [];
      if (i == 0) {
        t.push('Vencimento');
        t.push('N.');
        t.push('Valor');
        t.push('Saldo')
      } else {
        if (arrayTabela[i].vencimento != "") {
          t.push(arrayTabela[i].vencimento.split(',')[0]);
        }
        t.push(arrayTabela[i].nparcela);
        t.push(arrayTabela[i].parcela.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }));
        t.push(arrayTabela[i].saldo.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }));
        arrayFinal.push(t);
    }}

    body.getChild(childIndex).asText().setText('');
    var tab = body.insertTable(childIndex,arrayFinal);
    tab.setColumnWidth(0, 60);
    tab.setColumnWidth(1, 30);
    tab.setColumnWidth(2, 80);
    tab.setColumnWidth(3, 100);


    var table = body.getTables()[0]; // Please set your table.
    Logger.log(table);
    var index = body.getChildIndex(table);
    var tableIndex = Docs.Documents.doc.body.content[index + 1].startIndex;
    doc.saveAndClose();
    var requests = [{ updateTableCellStyle: { tableCellStyle: { borderRight: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } }, borderLeft: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } } }, tableStartLocation: { tableIndex }, fields: "borderRight,borderLeft" } }];

    Docs.Documents.batchUpdate({ requests }, docID);

}

UPDATE Changed the code as follows and got the following error:

requests[0].update_table_cell_style.table_start_location': Cannot find field.

var tableIndex = Docs.Documents.get(docID).body.content[index + 1].startIndex;
doc.saveAndClose();
var requests = [{ updateTableCellStyle: { tableCellStyle: { borderRight: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } }, borderLeft: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } } }, tableStartLocation: { tableIndex }, fields: "borderRight,borderLeft" } }];

Docs.Documents.batchUpdate({ requests }, docID);

Solution

  • Modification points:

    • About var tableIndex = Docs.Documents.doc.body.content[index + 1].startIndex;, I think that Docs.Documents has no property of doc.

    • About the following change, in this case, there is no property of tableIndex. By this, an error like requests[0].update_table_cell_style.table_start_location': Cannot find field. occurs.

      var tableIndex = Docs.Documents.get(docID).body.content[index + 1].startIndex;
      doc.saveAndClose();
      var requests = [{ updateTableCellStyle: { tableCellStyle: { borderRight: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } }, borderLeft: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } } }, tableStartLocation: { tableIndex }, fields: "borderRight,borderLeft" } }];
      
      Docs.Documents.batchUpdate({ requests }, docID);
      
    • I think that doc.saveAndClose() is required to be moved before Docs.Documents.get.

    When these points are reflected in your script, it becomes as follows.

    From:

    var table = body.getTables()[0]; // Please set your table.
    Logger.log(table);
    var index = body.getChildIndex(table);
    var tableIndex = Docs.Documents.doc.body.content[index + 1].startIndex;
    doc.saveAndClose();
    var requests = [{ updateTableCellStyle: { tableCellStyle: { borderRight: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } }, borderLeft: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } } }, tableStartLocation: { tableIndex }, fields: "borderRight,borderLeft" } }];
    
    Docs.Documents.batchUpdate({ requests }, docID);
    

    To:

    var table = body.getTables()[0];
    doc.saveAndClose();
    var index = Docs.Documents.get(docID).body.content[body.getChildIndex(table) + 1].startIndex;
    var requests = [{ updateTableCellStyle: { tableCellStyle: { borderRight: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } }, borderLeft: { dashStyle: "SOLID", width: { magnitude: 0, unit: "PT" }, color: { color: { rgbColor: { red: 0 } } } } }, tableStartLocation: { index }, fields: "borderRight,borderLeft" } }];
    Docs.Documents.batchUpdate({ requests }, docID);
    

    Note:

    • In this modification, it supposes that your values of docID, arrayTabela, texttofind are valid values. Please be careful about this.

    References: