简体   繁体   English

使用 nodejs 和 Google Sheets API v4 从工作表名称中查找 google sheet id

[英]Find google sheet id from sheet name using nodejs and Google Sheets API v4

I would like to find a google sheet id (as in the.../edit#gid="SHEET_ID" query parameter) from the sheet name using nodejs and the Google Sheets v4 API.我想使用 nodejs 和 Google Sheets v4 API 从工作表名称中找到一个 google sheet id(如 .../edit#gid="SHEET_ID" 查询参数)。 If I understand correctly the following code should work, but the response does not contain any "sheetIds" at all.如果我理解正确,以下代码应该可以工作,但响应根本不包含任何“sheetIds”。 Any idea what I've maybe missed?知道我可能错过了什么吗?

const request = {
    spreadsheetId: GS_ID,
    ranges: [sheetName],
    includeGridData: false
};
let res = await GS.spreadsheets.get(request);

console.log(res.data.properties.sheetId);

I only get this data, so the sheets properties seem to be missing我只得到这些数据,所以工作表属性似乎丢失了

{ spreadsheetId: '###',
  properties:
   { title: '###',
     locale: 'en_US',
     autoRecalc: 'ON_CHANGE',
     timeZone: 'Asia/Tokyo',
     defaultFormat: {...},
  sheets: [ { properties: [Object] } ],
  spreadsheetUrl: 'https://docs.google.com/spreadsheets/d/###/edit' }

Explanation:解释:

The ranges parameter needs to be in A1 notation to retrieve sheet information from the response. ranges参数需要采用 A1 表示法才能从响应中检索工作表信息。 To retrieve sheet information, iterate through the sheets array and get the sheetID from a particular sheet name:要检索工作表信息,请遍历工作表数组并从特定工作表名称获取工作表 ID:

Sample:样本:

const rangeName = 'A1:B1';
const sheetName = 'Sheet1';
const GS_ID = <insert spreadsheet ID here>;
const request = {
    spreadsheetId: GS_ID,
    ranges: rangeName,
    includeGridData: false
};
let res = await GS.spreadsheets.get(request);

for (i = 0; i < res.sheets.length; i++) {
  if (res.sheets[i].title === sheetName) {
    console.log(res.sheets[i].sheetId); // get sheet ID here
  }
}

References:参考:

Sheets API | 床单 API | Spreadsheets.get 电子表格.get

Object Spreadsheet Object 电子表格

Object Sheet Object 片材

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM