Removing Vertical Borders of Table in Google Document using Google Apps Script

Gists

These are sample scripts for removing the vertical borders of a table in Google Document using Google Apps Script.

Unfortunately, in the current stage, only the vertical borders cannot be removed from the table in Google Document using the Google Document service (DocumentApp). I believe that this might be resolved in the future update. But, in the current stage, when Google Docs API is used, this can be achieved. So, Google Docs API can be used as the current workaround. But, I thought that the combination of Google Document service (DocumentApp) and Google Docs API might be a bit complicated. So, I would like to introduce 3 sample scripts.

Usage

  • These sample scripts are used as the container-bound script of Google Document. So, please copy and paste the following scripts to the script editor of Google Document.
  • These sample scripts use Google Docs API. So, before you use scripts, please enable Google Docs API at Advanced Google services.

Sample script 1

In this sample, a new table without the vertical borders is created.

function sample1() {
  const numberRows = 3;
  const numberColumns = 5;
  const sampleAr = [...Array(numberRows)].map((_) =>
    Array(numberColumns).fill("")
  ); // If you want to include values, @;ease replace this to your actual values.

  const doc = DocumentApp.getActiveDocument();
  const docId = doc.getId();
  const body = doc.getBody();
  const table = body.appendTable(sampleAr);
  const i = body.getChildIndex(table);
  doc.saveAndClose();
  const index = Docs.Documents.get(docId).body.content[i + 1].startIndex;
  const requests = [
    {
      updateTableCellStyle: {
        tableStartLocation: { index },
        tableCellStyle: {
          borderRight: {
            dashStyle: "SOLID",
            width: { magnitude: 0, unit: "PT" },
            color: { color: {} },
          },
          borderLeft: {
            dashStyle: "SOLID",
            width: { magnitude: 0, unit: "PT" },
            color: { color: {} },
          },
        },
        fields: "borderRight,borderLeft",
      },
    },
  ];
  Docs.Documents.batchUpdate({ requests }, doc.getId());
}

When this script is run, the following result is obtained.

Sample script 2

In this sample, the vertical borders of the existing table are removed.

function sample2() {
  const doc = DocumentApp.getActiveDocument();
  const docId = doc.getId();
  const body = doc.getBody();
  const table = body.getTables()[0]; // In this sample, 1st table is used.
  const i = body.getChildIndex(table);
  const index = Docs.Documents.get(docId).body.content[i + 1].startIndex;
  const requests = [
    {
      updateTableCellStyle: {
        tableStartLocation: { index },
        tableCellStyle: {
          borderRight: {
            dashStyle: "SOLID",
            width: { magnitude: 0, unit: "PT" },
            color: { color: {} },
          },
          borderLeft: {
            dashStyle: "SOLID",
            width: { magnitude: 0, unit: "PT" },
            color: { color: {} },
          },
        },
        fields: "borderRight,borderLeft",
      },
    },
  ];
  Docs.Documents.batchUpdate({ requests }, docId);
}

When this script is run, the following result is obtained.

  • From

  • To

Sample script 3

In this sample, the sample values are put from row 2 as the insert rows, and the vertical borders are removed from the inserted rows.

function sample3() {
  const sampleValues = [
    ["a1", "b1", "c1"],
    ["a2", "b2", "c2"],
    ["a3", "b3", "c3"],
  ];
  const offset = 1; // Sample values are put from 2nd row.

  const doc = DocumentApp.getActiveDocument();
  const docId = doc.getId();
  const body = doc.getBody();
  const table = body.getTables()[0]; // In this sample, 1st table is used.
  sampleValues.forEach((r, i) => {
    const row = table.insertTableRow(offset + i);
    r.forEach((c, j) => row.appendTableCell(c));
  });
  const i = body.getChildIndex(table);
  doc.saveAndClose();
  const index = Docs.Documents.get(docId).body.content[i + 1].startIndex;
  var requests = [
    {
      updateTableCellStyle: {
        tableCellStyle: {
          borderRight: {
            dashStyle: "SOLID",
            width: { magnitude: 0, unit: "PT" },
            color: { color: {} },
          },
          borderLeft: {
            dashStyle: "SOLID",
            width: { magnitude: 0, unit: "PT" },
            color: { color: {} },
          },
        },
        tableRange: {
          tableCellLocation: {
            tableStartLocation: { index },
            rowIndex: offset,
          },
          rowSpan: sampleValues.length,
          columnSpan: sampleValues[0].length,
        },
        fields: "borderRight,borderLeft",
      },
    },
  ];
  Docs.Documents.batchUpdate({ requests }, docId);
}

When this script is run, the following result is obtained.

  • From

  • To

I answered this sample script to this thread of Stackoverflow.

References

 Share!