使用Google Apps脚本删除Google文档中的表格行 [英] Remove row of table in a Google Document with Google Apps Script

查看:149
本文介绍了使用Google Apps脚本删除Google文档中的表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create documents massively using information from a spreadsheet to Google using Google Apps Script, but I don't know how to use the Table class (specifically the method: RemoveRow)

为了说明我的问题,我创建了一个示例(不太复杂).我有一个Google文档,名为"销售报告",该文档从此处读取了信息名为数据的Google表格.到目前为止,我可以做到.

I created an example (less complex) in order to illustrate my problem. I have a Google document called "Sales Reports", this document read information from this Google Sheets called Data. So far no problem, I can do it.

但是,有条件.产品A和产品B是互斥的,也就是说:如果我购买产品A",那么我就不会购买产品B",反之亦然. Google文档必须显示1行(而不是2行).代码类似于以下内容:

However, there are a condition. The product A and product B are exclusive, that is: if I buy "product A" then, I do not buy the "product B" and vice versa. The Google Docs must show 1 row (not 2). The code is similar to this:

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文档的所有表中删除所有空行:

This function removes all of the empty rows from all of the tables in a Google Document:

/**
 * 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() 

这篇关于使用Google Apps脚本删除Google文档中的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆