skip to Main Content

I was getting problemas with my code to format the borders of a table in a Google Document and
Tanaike helpe me here

The problem now is that my table is not getting formatted, it is as if the code doesn`t find the table. It remains with the default borders. 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);
    }}
  //   var t = [
  //   ['Vencimento', 'N. Parcela', 'Valor'],
  //   ['Row 2, Cell 1', 'Row 2, Cell 2']
  // ];
    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];
    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);

}

2

Answers


  1. Chosen as BEST ANSWER

    Thanks all for your help. Got it working like this:

    const index = body.getChildIndex(tab);
    const documentId = doc.getId();
    doc.saveAndClose();
    const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
    const tempStyledel = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {red: 0.5, green : 0.5, blue: 0.5}}}};
    const tempStyle = {width: {magnitude :0.75, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {red: 0.5, green : 0.5, blue: 0.5}}}};
    const resource = {requests: [
      {
        "pinTableHeaderRows": { // Set pinned rows
          "tableStartLocation": {
            "index": tableStart
            },
          "pinnedHeaderRowsCount": 1
        }
      },
      {updateTableCellStyle: {
        tableStartLocation: {index: tableStart},
        tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyledel, borderRight: tempStyledel},
        fields: "borderTop,borderBottom,borderLeft,borderRight"
      }},
      {updateTableCellStyle: {
        tableRange: {
          tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 4},
          tableCellStyle: {
            backgroundColor: { color: { rgbColor: {red: 0.1098, green: 0.1490, blue: 0.3294 }}}
            // borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 1}}}}
          },
          fields: "backgroundColor"
      }}
    ]};
    Docs.Documents.batchUpdate(resource, documentId);
    

  2. You need to change your request.

    width: { magnitude: 0, unit: "PT" }  // means no thickness
    
    color: { color: { rgbColor: { red: 0 } } } }  // means no color
    

    Try the following:

    let requests = [
        { updateTableCellStyle: 
          { tableCellStyle: 
            { borderRight: 
              { dashStyle: "SOLID", 
                width: { magnitude: 1, unit: "PT" }, 
                color: 
                  { color: 
                    { rgbColor: { red: 1, green: 0, blue: 0 } } 
                  }
              }, 
              borderLeft: 
                { dashStyle: "SOLID", 
                  width: 
                    { magnitude: 1, unit: "PT" }, 
                  color: 
                    { color: { rgbColor: { red: 1, green: 0, blue: 0 } } } 
                } 
            }, 
            tableStartLocation: { index: index }, 
            fields: "borderRight,borderLeft" } 
        }
      ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search