简体   繁体   English

使用Apps脚本对Google Doc中的表格行进行编号

[英]Number table rows in Google Doc using Apps Script

I'm working on a script which takes text and places each paragraph in a numbered table cell. 我正在研究一个脚本,该脚本接受文本并将每个段落放在编号的表格单元格中。 I'm running into a problem where each line break is counted as a paragraph by the script, which means my table either has empty cells or is numbered incorrectly. 我遇到一个问题,脚本将每个换行符都计为一个段落,这意味着我的表要么具有空单元格,要么被编号错误。

Here's the working script (minus row numbering): 这是工作脚本(减去行编号):

function formatArticle() {
  var doc = DocumentApp.getActiveDocument()
  var body = doc.getBody(); 

  // Get the paragraphs
  var paras = body.getParagraphs();

  // Add a table to fill in with copied content
  var addTable = body.appendTable();
  for (var i=0;i<paras.length;++i) {

    // If the paragraph is text, add a table row and insert the content.
    if(i % 2 == 0) {
      var tr = addTable.appendTableRow();
      var text = paras[i].getText();

      // Number the table rows as they're added in a cell to the left.
      for(var j=0;j<2;j++) {
        if(j == 0) {
          var td = tr.appendTableCell(i);
          } else {
            var td = tr.appendTableCell(text);
          }
        }
      }

    // Shrink left column.
    addTable.setColumnWidth(0, 65);

    // Delete the original text from the document.
    paras[i].removeFromParent();
  }
}

Here's a demo Google Doc so you can see how text formats without setting a new one up yourself if that helps. 这是一个演示Google文档,因此您可以查看文本格式,而无需自己设置新格式(如果有帮助的话)。

Try this: tr.appendTableCell(i/2 +1) 试试这个: tr.appendTableCell(i/2 +1)

// Number the table rows as they're added in a cell to the left.
  for(var j=0;j<2;j++) {
    if(j == 0) {
      var td = tr.appendTableCell(i/2 +1);
      } else {
        var td = tr.appendTableCell(text);
      }
    }
  }

Logic 逻辑

What you should do is to iterate each paragraph and check if that paragraph contains text. 您应该做的是迭代每个段落,并检查该段落是否包含文本。 If yes, then add a row and put the text in it, else skip that paragraph and jump to the next one. 如果是,则添加一行并将其放在其中,否则跳过该段并跳至下一段。

Implementation 实作

function formatArticle(){ 
    var doc = DocumentApp.getActiveDocument()
    var body = doc.getBody(); 

    // Get the paragraphs
    var paras = body.getParagraphs();
    var addTable = body.appendTable();

    //paragraph index 
    var pIndex = 0;
    for(var i = 0 ; i < paras.length ; i++){
        var para = paras[i];
        var paraStr = para.editAsText().getText();
        // if there is string content in paragraph
        if(paraStr.length){
            var tr = addTable.appendTableRow();
            var td1 = tr.appendTableCell(pIndex);
            var td2 = tr.appendTableCell(paraStr);
            pIndex ++;
        }
        para.removeFromParent();
    }
    // Shrink left column.
    addTable.setColumnWidth(0, 65);
}

Here You get the string from paragraph var paraStr = para.editAsText().getText(); 在这里,您从段落var paraStr = para.editAsText().getText();获取字符串var paraStr = para.editAsText().getText(); and check if the content exists if(paraStr.length) If yes then create a row, insert paragraph index and paragraph's text in it. 并检查内容是否存在if(paraStr.length)如果是,则创建一行,在其中插入段落索引和段落文本。

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

相关问题 使用Apps脚本从Google电子表格添加到Google文档 - Adding to a Google Doc from a Google Spreadsheet Using Apps Script VBA到Google Doc Apps脚本 - VBA to Google Doc Apps Script 使用 Google Apps 脚本隐藏行 - Hiding Rows using Google Apps Script 如何使用 Google Apps Script 取消透视表? - How to unpivot a table using Google Apps Script? 如何使用 Google Apps Script 在来自 Google 表格的多行中拆分 Google 幻灯片表中的数据(基于字符限制)? - How to split data in a Google Slides Table (based on character limit) in multiple rows coming in from a Google Sheet using Google Apps Script? 正在寻找一种使用 Google Apps 脚本或 DOCS API 为 Google 文档中的段落添加边框的方法? - Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API? 如何使用Google Apps脚本将Google文档文本导入电子邮件草稿 - How to Import Google doc text into Email draft using Google Apps Script 使用 Google Apps Script 在 Google Doc 中查找包含带括号的文本的元素 - Find element containg text with brackets in Google Doc using Google Apps Script 使用Google Apps脚本自动生成Google表格的参考号 - Generate Reference Number to Google Sheets automatically using Google Apps Script 使用 Google Apps 脚本隐藏行 - Hide Rows with Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM