Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and select table from Document in Apps Script?

I'm creating a function in Google Apps Script. The purpose of this function is selecting the table from the document and move values to the created Spreadsheet. The problem is that I can't get the table from the document (debugging is OK, but logs show selected table as empty {}).

    function addAnswersTable() {
    var File = function(Path) { // File object constructor
        this.Path = Path;
        this.Doc = DocumentApp.openById(this.Path);
        this.getTable = new function()
        // This function defines 
        // a getTable method to get 
        // the first table in the Document
        {
            if (this.Doc != undefined) {
                var range = this.Doc.getBody();
                var tables = range.getTables();
                var table = tables[0];
                return table;
            }
        }
    }
    // Creating Excel Table, where first column
    // of selected table should be moved
    var Table = SpreadsheetApp.create("AnswersTable");
    // Creating new File object
    var TrueAnswersFile = new File
    ('1_ne9iBaK-Z36yUYrISr3gru3zw3Qdsneiu14sWnjn34');
    // Calling getTable method to get the table placed in File 
    var TrueAnswersTable = TrueAnswersFile.getTable;
    for (var i = 1; i <= TrueAnswersTable.getNumRows; i++) {
        // Filling spreadsheet "A" column with cells'
        // values ​​from table stored in File
        Table.getActiveSheet().getRange("A" + i).setValue(TrueAnswersTable.getCell(1, i).getValue());
    };
    }

I except the output in Spreadsheet column "A" like :

A1. Just
A2. Cells'
A3. List item with
A4. Values From Table

Actually spreadsheet is empty

like image 434
Dmitriy Mishenyov Avatar asked Oct 18 '25 16:10

Dmitriy Mishenyov


1 Answers

  • You want to retrieve the values from the column "A" of Google Document and put the values to the column "A" of the created Spreadsheet.
  • The table of index 0 in the Document has 4 rows and 1 column.
    • The values of each row is Just, Cells', List item with, Values From Table.

I could understand like above. If my understanding is correct, how about this modification?

Modification points:

  • In your script, the method is not used as the function. By this, the method is not run.
    • For example, TrueAnswersFile.getTable and TrueAnswersTable.getNumRows.
  • No method is used.
    • For example, getValue() of TrueAnswersTable.getCell(1, i).getValue().
  • new of this.getTable = new function() is not required.
  • In your script, getCell(1, i) of TrueAnswersTable.getCell(1, i) retrieves the values at from column "B" of the row 2.
    • If you want to retrieve the values from the row 1 of the column "A", please modify to getCell(i - 1, 0). But in this modification, the start of index is 0. So you can use getCell(i, 0).
  • When setValue() is used in the for loop, the process cost becomes high. In your case, you can use setValues() instead of it.

When above points are reflected to your script, it becomes as follows.

Modified script:

function addAnswersTable() {
  var File = function(Path) {
    this.Path = Path;
    this.Doc = DocumentApp.openById(this.Path);
    this.getTable = function() { // Modified
      if (this.Doc != undefined) {
        var range = this.Doc.getBody();
        var tables = range.getTables();
        var table = tables[0];
        return table;
      }
    }
  }
  var Table = SpreadsheetApp.create("AnswersTable");
  var TrueAnswersFile = new File('1_ne9iBaK-Z36yUYrISr3gru3zw3Qdsneiu14sWnjn34');
  var TrueAnswersTable = TrueAnswersFile.getTable();
  var values = []; // Added
  for (var i = 0; i < TrueAnswersTable.getNumRows(); i++) { // Modified
    values.push([TrueAnswersTable.getCell(i, 0).getText()]) // Modified
  };
  Table.getRange("A1:A" + values.length).setValues(values); // Added
}

References:

  • getCell(rowIndex, cellIndex)
  • getText()
  • Benchmark: Reading and Writing Spreadsheet using Google Apps Script
like image 89
Tanaike Avatar answered Oct 20 '25 05:10

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!