0
votes

Hi if you want to use sortRange in Google Sheets API you need a sheetId as an int not string. 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: 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? It's in the URL but sadly batchUpdate, clear or append do not have it as an int in the response

I have the spreadsheet ID and the table name as sting.

2
Read the usage guideline at the top of google-sheets. Use appropriate tags.TheMaster

2 Answers

2
votes

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

sheetname is the title of the sheet as a string

//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)
-1
votes

You can use the getSheetid method. This will return the id of the sheet as a string:

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

Logger.log(sheet.getSheetId());