Search code examples
javascriptgoogle-apps-scriptgoogle-docs

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?


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

Solution

  • Modification points:

    • About your provided sample Document of https://docs.google.com/document/d/###/edit?rtpof=true&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() >= 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());
          }
        }
      }
    }
    
    • 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 < row.getNumCells(); c++) {
            row.getCell(c).setText("");
          }
          for (let i = 0; i < 20; i++) {
            table.appendTableRow(row.copy());
          }
        }
      }
      

    References: