1

I'm working on a script to automate the creation of a report, this script creates a google document and fills it with information from different sources, the report has a table that is copied from a Google Spreadsheet using the following code:

  // get the Spreadsheet by sheet id
  var source = SpreadsheetApp.openById(OPERATIONS_FILE);
  // select the sheet
  var sourcesheet = source.getSheetByName(OPERATION_SHEET);
  // get the values on selectd range
  var srcData = sourcesheet.getRange(OPERATION_RANGE).getValues();




  // Set the variable templates on the new Report
  var newReport = DocumentApp.openById( reportFile.getId() );

  var reportBody = newReport.getBody();
      reportBody.replaceText('{date}', dateForTitle);
      reportBody.replaceText('{workWeek}', weekNumber[1]);


  // Get the data from the Dashboard and put it on the Report
  var range = reportBody.findText("{operation}");

  var operationsTable = "";

  var ele = range.getElement();
  if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) {
    var offset = reportBody.getChildIndex(ele.getParent());
    operationsTable = reportBody.insertTable(offset + 1, srcData);
  }

Now the table inserted into the google document 'operationsTable' contains extra columns that I need to delete (those are needed on the spreadsheet for calculation purposes) I found RemoveRow(I) function on google script documentation but couldn't found RemoveColumn(I) how can you delete certain columns of a Table once is on Google Document?

  • Can you please give more context, and show the code you are using? How are you copying the table from the spreadsheet? Is there a reason you cannot move that calculation column to a different location? – Stykes May 20 '20 at 16:50

1 Answers1

1

I suggest deleting those elements after you getValues() but before you put those values into the GoogleDoc

Since getValues() returns a 2D array, and you want to remove a column from that 2D array, meaning one of the elements in every row. This answer shows how to do that using .map() and .slice(). Both methods take some time to get used to so practice with a simple data set first.

Stykes
  • 306
  • 6
  • 15