且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Google表格行中获取列的序号索引?

更新时间:2023-10-24 23:53:28

Sheetsv4中没有这样的功能。您必须手动执行
在这里我正在寻找手机的位置,它是C3

 函数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; //注意:不是oReq.responseText
console.log(arrayBuffer+ arrayBuffer);
myArr = JSON.parse(arrayBuffer); (myArr.values [0] [i] ===

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

电话){
console.log(列位置是+(i + 1)); // +1,因为计数从0开始
}
}
};
xhr.send(null);
}

输出:



可能还有其他方法可以做到这一点。我刚给你看我的版本。


I'm trying to figure out the most robust way to determine the ordinal position of a column in a google sheet from a row feed, not a cell feed.

So, for example, I have the following spreadsheet:

                        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 |
              +----+---------+-----------+---------+---------+

In my code, I'm able to successfully fetch a row-based feed from the sheet via Google's Sheet's API. The object I get back for each row in the feed includes various properties, most of which are the values in the header row. So for example, a single row object looks something like:

{
    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'
}

I want to find the ordinal position of any given column from the row-based feed, but it doesn't seem immediately obvious how to do this. It does seem the object's properties are ordered according to their position in the header row, but I'm hard pressed to find which column number the property refers to.

The reason I want the column number is that I want to get a cells-based feed for a particular column and row, but it appears Google's API requires that you know the ordinal position of the column. That is, you need to know the min-column and max-column, as well as a min-row and max-row for a cells based feed. Now, I know that I can obviously look at the column in the Sheets UI and determine which position the column is in, but how can I do it programatically?

In sum, I'm trying to find a programmatic way to answer a simple question such as question:

"What is the ordinal position of the phone column?"

Is this even possible with Google's row-feed response?

There is no such feature yet in Sheetsv4. You have to manually perform the Basic Reading methods and parse the response and loop through it.

Here I was looking for the position of "phone" which is 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);
      }

Output:

There may be other ways to do this. I just showed you my version.