【问题标题】:Remove row of table in a Google Document with Google Apps Script使用 Google Apps 脚本删除 Google 文档中的表格行
【发布时间】:2015-11-12 22:51:03
【问题描述】:

我正在尝试使用 Google Apps 脚本从电子表格到 Google 的信息大量创建文档,但我不知道如何使用 Table 类(特别是方法:RemoveRow)

我创建了一个示例(不太复杂)来说明我的问题。我有一个名为“Sales Reports”的 Google 文档,该文档从名为 Data 的 Google 表格中读取信息。到目前为止没问题,我可以做到。

但是,有一个条件。产品A和产品B是互斥的,即:如果我购买“产品A”,那么我不购买“产品B”,反之亦然。 Google 文档必须显示 1 行(不是 2 行)。代码类似这样:

var TEMPLATE_ID = 'xyz';
function createPdf() {
  var activeSheet = SpreadsheetApp.getActiveSheet(),
  numberOfColumns = activeSheet.getLastColumn(),
  numberOfRows = activeSheet.getLastRow(),
  activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
  activeRow = '',
  headerRow = activeSheet.getRange(1, 1, numberOfRows, numberOfColumns).getValues()
  var destFolder = DriveApp.getFolderById("aaaaaaa");

 for(i=2;i<=numberOfRows;i++) {
 var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy()
 copyId = copyFile.getId(),
 copyDoc = DocumentApp.openById(copyId),
 copyBody = copyDoc.getActiveSection()
 activeRow = activeSheet.getRange(i, 1, 1, numberOfColumns).getValues();

 for (columnIndex = 0;columnIndex < headerRow[0].length; columnIndex++) {
    copyBody.replaceText('%' + headerRow[0][columnIndex] + '%', 
                     ''+activeRow[0][columnIndex]);    
}


copyDoc.saveAndClose()

                              .....
            //code: create pdf with the custom template

}

} //fin del crear

我需要做哪些更改?

您好,提前致谢。

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    此函数从 Google 文档中的所有表中删除所有空行:

    /**
     * Remove all the empty rows from all the tables in a document
     *
     * @param {String} documentId
     */
    
    function removeEmptyRows(documentId) {
    
      var tables = DocumentApp.openById(documentId).getBody().getTables()
    
      tables.forEach(function(table) {
    
        var numberOfRows = table.getNumRows()
    
        for (var rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
    
          var nextRow = table.getRow(rowIndex)
          var numberOfColumns = nextRow.getNumCells()
    
          // A row is assumed empty until proved otherwise
          var foundEmptyRow = true
    
          for (var columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {  
    
            if (nextRow.getCell(columnIndex).getText() !== '') {
              foundEmptyRow = false
              break
            }
    
          } // for each column
    
          if (foundEmptyRow) {
            table.removeRow(rowIndex)
            numberOfRows--
          }
    
        } // For each row
      })
    
    } // removeEmptyRows() 
    

    【讨论】:

      【解决方案2】:

      我使用了你的代码,但你忘记了一个元素

      rowIndex--;

      在街区

        if (foundEmptyRow) {
          table.removeRow(rowIndex)
          numberOfRows--
        }
      

      有了这个就很好了。

        if (foundEmptyRow) {
          table.removeRow(rowIndex)
          numberOfRows--
          rowIndex--
        }
      

      【讨论】:

      • 松散 - 没有牢固或紧密固定到位;分离或能够分离。 “一颗松动的牙齿”
      猜你喜欢
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2021-10-22
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多