Academy ↓
Get Sheet ID and Tab GID Into Your Google Sheet
The function retrieves and writes the active spreadsheet ID and the specific sheet ID of "Sheet1" into cells A1 and A2 of "Sheet1", respectively. This can be useful for referencing or logging purposes.
Retrieve the Spreadsheet ID:
The script retrieves the ID of the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet().getId(). This ID uniquely identifies the spreadsheet.
Write the Spreadsheet ID to Cell A1:
The script accesses the sheet named "Sheet1" and sets the value of cell A1 to the retrieved spreadsheet ID.
Retrieve the Sheet ID of "Sheet1":
The script retrieves the ID of the sheet named "Sheet1" using SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getSheetId(). This ID uniquely identifies the sheet within the spreadsheet.
Write the Sheet ID to Cell A2:
The script sets the value of cell A2 in "Sheet1" to the retrieved sheet ID.
function getSheetIDofSheet1() {
var id = SpreadsheetApp.getActiveSpreadsheet().getId()
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1").setValue(id)
var gid = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getSheetId()
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A2").setValue(gid)
}
function getSheetIDofSheet2() {
var id = SpreadsheetApp.getActiveSpreadsheet().getId()
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("A1").setValue(id)
var gid = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getSheetId()
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("A2").setValue(gid)
}