Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets 如何从GoogleSheets行提要获取列的顺序索引?_Google Sheets_Google Sheets Api - Fatal编程技术网

Google sheets 如何从GoogleSheets行提要获取列的顺序索引?

Google sheets 如何从GoogleSheets行提要获取列的顺序索引?,google-sheets,google-sheets-api,Google Sheets,Google Sheets Api,我试图找出一种最可靠的方法,通过行提要而不是单元格提要来确定google工作表中列的顺序位置 例如,我有以下电子表格: A B C D +----+-----------------------------------------+ Header Row | 1 | name | address | phone | email |

我试图找出一种最可靠的方法,通过行提要而不是单元格提要来确定google工作表中列的顺序位置

例如,我有以下电子表格:

                        A          B          C         D     
              +----+-----------------------------------------+
 Header Row   | 1  |  name   |  address  |  phone  |  email  |
              +----+---------+-----------+---------+---------|
              | 2  | Jane    | 1001      | 6139023 | w@gmail |
              +----+---------+-----------+---------+---------+
              | 3  | Jon     | 1102      | 4320909 | x@gmail |
              +----+---------+-----------+---------+---------+
              | 4  | Jeff    | 1203      | 4132323 | y@gmail |
              +----+---------+-----------+---------+---------+
              | 5  | Jenny   | 1304      | 3346044 | z@gmail |
              +----+---------+-----------+---------+---------+
在我的代码中,我能够通过谷歌的工作表API成功地从工作表中获取基于行的提要。我为提要中的每一行返回的对象包括各种属性,其中大多数是标题行中的值。例如,一个单行对象看起来像:

{
    id           : 'https://spreadsheets.google.com/...'
  , 'app:edited' : '2017-02-24T23:59:56:56.520Z'
  , _links       : [ self: 'https://...', edit: 'https://...']
  , name         : 'Jane'
  , address      : '1001'
  , phone        : '6139023'
  , email        : 'w@gmail'
}
我想从基于行的提要中找到任何给定列的顺序位置,但如何做到这一点似乎并不明显。看起来对象的属性是根据它们在标题行中的位置排序的,但是我很难找到属性所指的列号

我想要列号的原因是我想为特定的列和行获取基于单元格的提要,但Google的API似乎要求您知道列的顺序位置。也就是说,您需要知道基于单元格的提要的最小列和最大列,以及最小行和最大行。现在,我知道我可以在Sheets UI中查看列并确定列的位置,但是如何通过编程实现呢

总之,我正试图找到一种程序化的方式来回答一个简单的问题,如问题:

“电话列的顺序位置是什么?”


谷歌的行提要响应是否可能实现这一点?

Sheetsv4中还没有这一功能。您必须手动执行和解析响应,并通过它进行循环

在这里,我正在寻找“电话”的位置,它是C3

  function findPosition(){
         var arrayBuffer;
         var myArr;
         var xhr = new XMLHttpRequest();
         xhr.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/'+myspreadsheetId+'/values/'+"Sheet1!"+A+":"+Z);
         xhr.setRequestHeader('Authorization', 'Bearer ' + myAccessToken);

         xhr.onload = function (oEvent) {
             arrayBuffer = xhr.response; // Note: not oReq.responseText
             console.log("arrayBuffer "+ arrayBuffer);
             myArr = JSON.parse(arrayBuffer);

              for(var i = 0; i < myArr.values.length; i++){

                 if(myArr.values[0][i] === "phone"){
                     console.log("column position is " + (i+1)); // +1 because counting starts at 0
                 }
              }
         };
         xhr.send(null);
      }
函数findPosition(){
var arrayBuffer;
var myArr;
var xhr=new XMLHttpRequest();
xhr.open('GET','https://sheets.googleapis.com/v4/spreadsheets/“+myspreadsheetId+”/values/“+”Sheet1!”+A+“:”+Z);
setRequestHeader('Authorization'、'Bearer'+myAccessToken);
xhr.onload=函数(oEvent){
arrayBuffer=xhr.response;//注意:不是oReq.responseText
log(“arrayBuffer”+arrayBuffer);
myArr=JSON.parse(arrayBuffer);
对于(变量i=0;i
输出:


也许还有其他方法可以做到这一点。我刚刚向您展示了我的版本。

如果有很多行,例如100000行,那么这个解决方案是否需要在解析列之前“下载”整个工作表?