yay*_*wei 6 google-drive-api gsuite
使用Google Sheets REST API V4,并且希望使用工作表ID而不是工作表标题来查找单元格中的值。您似乎可以使用范围指定电子表格中的工作表,例如Sheet4!A1:Z500,但不能指定工作表ID(例如)1310487470!A1:Z500。这样,如果有人重命名工作表,查询就不必更改。Google API是否支持按ID查询?
当前查询:
https://sheets.googleapis.com/v4/spreadsheets/1SR0DJ4nV5-05EWxjz1OYOWkKFObClmsC0rOowPnMwNE/values/Sheet4!A1:Z500
理想查询:
https://sheets.googleapis.com/v4/spreadsheets/1SR0DJ4nV5-05EWxjz1OYOWkKFObClmsC0rOowPnMwNE/values/1310487470!A1:Z500
接下来的样本怎么样?我将其准备为GAS样品。如果您使用此功能,请在API控制台上启用Sheet API v4。
这将使用GID检索电子表格中工作表的数据。
检索电子表格中工作表的信息。创建具有GID键和工作表名称值的JSON数据。
使用JSON从工作表中检索数据。
示例脚本:
function fetch(url){
return UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
});
}
function main(){
// Retrieves information of sheets in spreadsheet.
var spreadsheet = "#####";
var url = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheet + "?fields=sheets(properties(sheetId%2Ctitle))";
var sheetdic = {};
JSON.parse(fetch(url)).sheets.forEach(function(e){
sheetdic[e.properties.sheetId] = e.properties.title;
});
// Retrieves data from sheet using GID.
var sheet = sheetdic["#####"]; // Imports sheet name using GID.
var url = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheet + "/values/" + sheet + "!A1:Z500";
var results = fetch(url);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1711 次 |
| 最近记录: |