简体   繁体   English

Google App 脚本删除 GDoc 中的表列

[英]Google App Script Delete Table Column in a GDoc

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?现在插入到谷歌文档“操作表”中的表格包含我需要删除的额外列(电子表格中需要这些列用于计算目的)我在谷歌脚本文档上找到了 RemoveRow(I) function 但找不到 RemoveColumn(I)一旦在谷歌文档上,你如何删除表格的某些列?

I suggest deleting those elements after you getValues() but before you put those values into the GoogleDoc我建议在getValues()之后但在将这些值放入 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.由于getValues()返回一个二维数组,并且您想从该二维数组中删除一列,这意味着每一行中的一个元素。 This answer shows how to do that using .map() and .slice() . 这个答案显示了如何使用.map().slice()来做到这一点。 Both methods take some time to get used to so practice with a simple data set first.这两种方法都需要一些时间来习惯,因此首先使用简单的数据集进行练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM