获取表单元格值Google Docs [英] Get Table Cell Values Google Docs

查看:144
本文介绍了获取表单元格值Google Docs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带有表格初始化的Google文档,我们需要将表格单元格中的值转换为数组。该表格在Google文档中如下所示:

We have a Google Document with a Table init, where we need to get the values inside the table cells into an Array. The table looks like this in Google Doc:

我找到了一种方法来记录具有以下代码的单元格:

I found a way to logg the values in the cells with this code:

 var searchElement = copyBody.findElement(DocumentApp.ElementType.TABLE);
     var element = searchElement.getElement();
     var table = element.asTable();
     var tablerows = element.getNumRows();

        for ( var row = 0; row < tablerows; ++row ) {
          var tablerow = element.getRow(row)
          for ( var cell=0; cell < tablerow.getNumCells(); ++cell) {


            var celltext = tablerow.getChild(cell).getText();
           Logger.log( "Text is ("+celltext+")" );

          }
        }

我们怎样才能将这些变成一个看起来像这样的数组:

How can we get these into an array that looks something like this:

   ['A', 'C', 'E', 'X'],
   ['Row 2, Cell 1 value', 'Row 2, Cell 2 value', 'Row 2, Cell 3 value', 'Row 2, Cell 4 value'],
   ['Row 3, Cell 1 value', 'Row 3, Cell 2 value', 'Row 3, Cell 3 value', 'Row 3, Cell 4 value']


推荐答案

你似乎在90%的路上。如果您真正想做的就是将这些单元格放入阵列中,我很惊讶您无法进行下一步操作?请允许我:

You seem to be 90% the way there. If all you really want to do is get those cells into an array I'm surprised you can't make the next step? Allow me:

var array = [];
for ( var row = 0; row < tablerows; ++row ) {
  var tablerow = element.getRow(row)
  array[row] = [];
  for ( var cell=0; cell < tablerow.getNumCells(); ++cell) {
    var celltext = tablerow.getChild(cell).getText();
    array[row][cell] = celltext;
  }
}
Logger.log(JSON.stringify(array)); // debug

这篇关于获取表单元格值Google Docs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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