从命名 sheet 获取 ID
getID from named sheet
有没有办法从命名的 sheet 中获取 ID(字符串)?我正在尝试将过滤器应用于命名 sheet。我见过的所有例子都是从活动sheet中获取id。但我想将它用于命名 sheet。我该怎么做?
var resultSheet = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Result");
Sheets.Spreadsheets.batchUpdate({'requests': request}, resultSheet.getSheetId());
使用:
var sheetID = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Your sheet name").getSheetId();
sheetID 将包含带有 sheet 的 ID
的字符串
在此处查看文档:https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getsheetid
Google 表格有两种相关的 "ID" 类型 - 一种对应于当前打开的 Google 表格文件 - spreadsheetId
which is also the exact same value as the Google Drive fileId
- and the other corresponds to a specific Sheet
within the Google Sheets spreadsheet, and is interchangeably known as a gridId
or sheetId
.
从给定的 Sheet
对象,您可以使用 class 方法 getParent()
to obtain a reference to the enclosing Spreadsheet
object. A Spreadsheet
has the method getId()
which will return the needed fileId
/spreadsheetId
for the Sheets API batchUpdate
方法。
已有对电子表格的引用是很常见的:
const wkbk = SpreadsheetApp.getActive();
const sheet = wkbk.getSheetByName("some name");
// ...
Sheets.Spreadsheets.batchUpdate({'requests': request}, wkbk.getId());
根据您的代码结构,此引用可能超出范围,并且您可能不想/不能在函数的参数列表中包含相关 ID:
function foo() {
const wkbk = SpreadsheetApp.getActive();
const wkbkId = wkbk.getId();
// ...
doSheetUpdate_(wkbk.getSheetByName("some name"));
// ...
}
function doSheetUpdate_(sheet) {
const wkbkId = sheet.getParent().getId();
const rq = getRequest_(sheet.getSheetId());
// ...
const resp = Sheets.Spreadsheets.batchUpdate({'requests': rq}, wkbkId);
}
function getRequest_(sheetId) {
// ...
}
感谢 post,我得到了我想要的答案。这是我可以使用的代码。
var resultSheet = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Result");
var myID = resultSheet.getParent().getID();
Sheets.Spreadsheets.batchUpdate({'requests': request}, myID);
这将帮助我避免 sheet 切换。
有没有办法从命名的 sheet 中获取 ID(字符串)?我正在尝试将过滤器应用于命名 sheet。我见过的所有例子都是从活动sheet中获取id。但我想将它用于命名 sheet。我该怎么做?
var resultSheet = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Result");
Sheets.Spreadsheets.batchUpdate({'requests': request}, resultSheet.getSheetId());
使用:
var sheetID = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Your sheet name").getSheetId();
sheetID 将包含带有 sheet 的 ID
的字符串在此处查看文档:https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getsheetid
Google 表格有两种相关的 "ID" 类型 - 一种对应于当前打开的 Google 表格文件 - spreadsheetId
which is also the exact same value as the Google Drive fileId
- and the other corresponds to a specific Sheet
within the Google Sheets spreadsheet, and is interchangeably known as a gridId
or sheetId
.
从给定的 Sheet
对象,您可以使用 class 方法 getParent()
to obtain a reference to the enclosing Spreadsheet
object. A Spreadsheet
has the method getId()
which will return the needed fileId
/spreadsheetId
for the Sheets API batchUpdate
方法。
已有对电子表格的引用是很常见的:
const wkbk = SpreadsheetApp.getActive();
const sheet = wkbk.getSheetByName("some name");
// ...
Sheets.Spreadsheets.batchUpdate({'requests': request}, wkbk.getId());
根据您的代码结构,此引用可能超出范围,并且您可能不想/不能在函数的参数列表中包含相关 ID:
function foo() {
const wkbk = SpreadsheetApp.getActive();
const wkbkId = wkbk.getId();
// ...
doSheetUpdate_(wkbk.getSheetByName("some name"));
// ...
}
function doSheetUpdate_(sheet) {
const wkbkId = sheet.getParent().getId();
const rq = getRequest_(sheet.getSheetId());
// ...
const resp = Sheets.Spreadsheets.batchUpdate({'requests': rq}, wkbkId);
}
function getRequest_(sheetId) {
// ...
}
感谢 post,我得到了我想要的答案。这是我可以使用的代码。
var resultSheet = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Result");
var myID = resultSheet.getParent().getID();
Sheets.Spreadsheets.batchUpdate({'requests': request}, myID);
这将帮助我避免 sheet 切换。