How to write some Google Apps Script code for increasing the number of rows of every table from 20 to 40 in a Google Docs file easily?

huangapple go评论74阅读模式
英文:

How to write some Google Apps Script code for increasing the number of rows of every table from 20 to 40 in a Google Docs file easily?

问题

以下是翻译好的部分:

这是文件。应该是一个工作表。

文件中有很多表格。每个表格有20个空白行(用于20名学生)。我需要40个空白行(用于40名学生)。

手动添加这些行是一项重复性的琐碎任务。我找到了一个相对简单的方法。使用鼠标选择这20行,然后右键单击。然后会出现一个选项,询问是否要添加20行在下面,但对于所有表格来说,仍然是一个繁琐的过程。如何更轻松地完成这项任务?

我不懂JavaScript,但还是愚蠢地尝试了以下代码:

function myFunction() {      
    
  for (let table in DocumentApp.getActiveDocument().getTables()) {      
    
    for (let i = 0; i < 20; i++) {              
      table.insertTableRow(20); 
    
    }   
    
  }

}

给我以下错误:

错误类型错误:table.insertTableRow不是一个函数。

英文:

This is the file. It is supposed to be a worksheet.

There are lots of tables in the file. Each table has 20 blank rows (for 20 students). I need 40 blank rows (for 40 students).

Adding the rows manually is a menially repetitive task. I found a relatively easy method. Select the 20 rows using the mouse, and then right click. Then an option comes up asking if I want to add 20 rows below or not, but it is still a cumbersome process to repeat for all the tables. How do I accomplish this task more easily?

I don't know the JavaScript, but still I stupidly tried the following code:

function myFunction() {      

  for (let table in DocumentApp.getActiveDocument().getTables()) {      

    for (let i = 0; i < 20; i++) {              
      table.insertTableRow(20); 

    }   

  }

}

Gives me the following error:

Error TypeError: table.insertTableRow is not a function

答案1

得分: 3

Here is the translated content:

修改点:

  • 关于您提供的 https://docs.google.com/document/d/###/edit?rtpof=true&sd=true 示例文档,从您的文件ID长度来看,当我检查mimeType时,似乎mimeType适用于DOCX数据。不幸的是,从您的问题中,我无法了解您的实际情况。在使用Google Apps Script处理DOCX文件时,不幸的是,在当前阶段,Document服务(DocumentApp)无法用于DOCX数据。请注意这一点。

    • 在这个答案中,假设您的脚本是绑定到Google文档的脚本。
  • 在您的脚本中,DocumentApp.Document类中没有 getTables() 方法。

  • 而且,for (let table in DocumentApp.getActiveDocument().getTables()) { 可能是 for (let table of DocumentApp.getActiveDocument().getTables()) {。在 let table in DocumentApp.getActiveDocument().getTables() 的情况下,table 是字符串类型的索引。

    • 我认为这可能是您当前问题的原因,即 table.insertTableRow is not a function
  • 当我查看您提供的DOCX文件时,该表格有23行。您想要从第20行插入20行吗?如果您想要在表格中添加20行,appendTableRow 可能会有用。

当这些点反映在您的脚本中时,以下修改如何?

修改后的脚本:

请将以下脚本复制并粘贴到您的Google文档的脚本编辑器中,并保存脚本。

function myFunction() {
  const body = DocumentApp.getActiveDocument().getBody();
  const tables = body.getTables();
  for (let table of tables) {
    if (table.getNumRows() >= 20) {
      const row = table.getRow(19).copy();
      for (let c = 0; c < row.getNumCells(); c++) {
        row.getCell(c).setText("");
      }
      for (let i = 0; i < 20; i++) {
        table.appendTableRow(row.copy());
      }
    }
  }
}
  • 当运行此脚本时,如果表格行数超过20行,将向表格追加新的20行。

  • 如果您想要从第20行插入新的20行,请进行如下修改。

    • table.appendTableRow(row.copy());

    • table.insertTableRow(20, row.copy());

  • 或者,如果您想要将新的20行附加到所有表格,可以如下修改:

    function myFunction() {
      const body = DocumentApp.getActiveDocument().getBody();
      const tables = body.getTables();
      for (let table of tables) {
        const row = table.getRow(table.getNumRows() - 1).copy();
        for (let c = 0; c < row.getNumCells(); c++) {
          row.getCell(c).setText("");
        }
        for (let i = 0; i < 20; i++) {
          table.appendTableRow(row.copy());
        }
      }
    }
    

参考:

英文:

Modification points:

  • About your provided sample Document of https://docs.google.com/document/d/###/edit?rtpof=true&amp;sd=true, from the length of your file ID, when I check the mimeType, it seems that the mimeType is for the DOCX data. Unfortunately, from your question, I cannot know your actual situation. When you use DOCX file using Google Apps Script, unfortunately, in the current stage, Document service (DocumentApp) cannot be used for DOCX data. Please be careful about this.

    • In this answer, it supposes that your script is the container-bound script of your Google Document.
  • In your script, there is no method of getTables() in Class DocumentApp.Document.

  • And, for (let table in DocumentApp.getActiveDocument().getTables()) { might be for (let table of DocumentApp.getActiveDocument().getTables()) {. In the case of let table in DocumentApp.getActiveDocument().getTables(), table is the index of the string type.

    • I think that this might be the reason for your current issue of table.insertTableRow is not a function.
  • When I saw your provided DOCX file, the table has 23 rows. Do you want to insert 20 rows from 20 rows? If you want to add 20 rows to the table, appendTableRow might be useful.

When these points are reflected in your script, how about the following modification?

Modified script:

Please copy and paste the following script to the script editor of your Google Document and save the script.

function myFunction() {
  const body = DocumentApp.getActiveDocument().getBody();
  const tables = body.getTables();
  for (let table of tables) {
    if (table.getNumRows() &gt;= 20) {
      const row = table.getRow(19).copy();
      for (let c = 0; c &lt; row.getNumCells(); c++) {
        row.getCell(c).setText(&quot;&quot;);
      }
      for (let i = 0; i &lt; 20; i++) {
        table.appendTableRow(row.copy());
      }
    }
  }
}
  • When this script is run, when the table has more than 20 rows, new 20 rows are appended to the table.

  • If you want to insert new 20 rows from 20 rows, please modify as follows.

    • From

        table.appendTableRow(row.copy());
      
    • To

        table.insertTableRow(20, row.copy());
      
  • Or, if you want to append new 20 rows to all tables, how about the following modification?

    function myFunction() {
      const body = DocumentApp.getActiveDocument().getBody();
      const tables = body.getTables();
      for (let table of tables) {
        const row = table.getRow(table.getNumRows() - 1).copy();
        for (let c = 0; c &lt; row.getNumCells(); c++) {
          row.getCell(c).setText(&quot;&quot;);
        }
        for (let i = 0; i &lt; 20; i++) {
          table.appendTableRow(row.copy());
        }
      }
    }
    

References:

huangapple
  • 本文由 发表于 2023年5月21日 14:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定