简体   繁体   English

如何在 Javascript 中使用 Google 表格 API 获取 sheetId?

[英]How to get sheetId with Google Sheets API in Javascript?

Hi if you want to use sortRange in Google Sheets API you need a sheetId as an int not string.嗨,如果您想在 Google 表格 API 中使用 sortRange,您需要一个 sheetId 作为 int 而不是字符串。 How can I get it starting with the sheets name as string如何以工作表名称作为字符串开始

This works perfectly to sort my the sheet.这非常适合对我的工作表进行排序。 But sheetId I had to set manually.但是 sheetId 我必须手动设置。 sheetId: 959061457工作表编号:959061457

try {
    const request = {
        spreadsheetId: spreadsheetId,
        resource: {
            requests: [{
                sortRange: {
                    range: {
                        sheetId: 959061457,
                        startRowIndex: 1,
                        endRowIndex: 1000,
                        startColumnIndex: 0,
                        endColumnIndex: 6
                    },
                    sortSpecs: [{
                        sortOrder: 'DESCENDING',
                        dimensionIndex: 2
                    }]
                }
            }],
        },
        auth: client,
    };

    await sheets.spreadsheets.batchUpdate(request)
} catch (error) {
    console.log(error)
}

Is there an option to get sheetId as a number from some api?是否可以从某些 api 中获取 sheetId 作为数字? It's in the URL but sadly batchUpdate, clear or append do not have it as an int in the response它在 URL 中,但遗憾的是 batchUpdate、clear 或 append 在响应中没有它作为 int

I have the spreadsheet ID and the table name as sting.我有电子表格 ID 和表名作为 sting。

It works with https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get它适用于https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get

sheetname is the title of the sheet as a string sheetname 是工作表的标题,为字符串

//Get sheetId by name
try {
    const request = {
        spreadsheetId: spreadsheetId,
        ranges: [sheetname],
        includeGridData: false,  
        auth: client,
    };

    res = await sheets.spreadsheets.get(request)
} catch (error) {
    console.log("Error get sheetId")
}

console.log(res.data.sheets[0].properties.sheetId)

You can use the getSheetid method.您可以使用getSheetid方法。 This will return the id of the sheet as a string:这会将工作表的 id 作为字符串返回:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log(sheet.getSheetId());

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

相关问题 如何获取Google Sheets API追加响应? - How to get google sheets api append response? 如何在 Javascript 中将查询字符串传递给 Google Sheets API v4 - How to Pass a Query String to Google Sheets API v4 in Javascript 如何使用Google表格API在JavaScript中创建包含两个表格的Google电子表格? - How Can I create a Google spreadsheet with two sheets in javascript using Google sheets API? Google表格API:如何使用JavaScript检索有效范围的A1表示法 - Google sheets API: How to retrieve the A1 notation of an active range with javascript 如何使用Google表格API + Javascript更新电子表格 - How to update spreadsheet using Google Sheets API + Javascript 谷歌表 API JavaScript:访问表与授权 - Google sheets API JavaScript: Access sheet with auth Google表格API读取单元格(Javascript) - Google Sheets API read cells (Javascript) 如何在 React 中使用 Google 表格 api 动态获取电子表格中的表格列表? - How do I get list of sheets in a spreadsheet dynamically with google sheets api in React? 从没有表格的 Google 表格中获取数据 API - Get data from Google Sheets without Sheets API 如何使用谷歌表 api v4 javascript 删除谷歌表中的空白行? - How to remove a blank row in google sheet using google sheets api v4 javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM