【发布时间】:2020-07-24 15:27:46
【问题描述】:
我正在尝试从 Bigquery 获取数据并使用 App 脚本将它们显示到我的电子表格中。首先,我在 G 盘中创建了一个电子表格文件,然后将我的代码放入脚本编辑器中。
这是我用来从 Bigquery 获取所有数据集的代码:
function getAllDataSets(filter){
try{
let req_for_datasets = BigQuery.Datasets.list(PROJECT_ID);
let datasets = req_for_datasets.datasets;
let list = [];
datasets.forEach( obj => {
if(obj.datasetReference.datasetId.indexOf(filter)>-1)
list.push(obj.datasetReference.datasetId);
});
return list;
}catch(e){
return [];
}
}
这个脚本运行良好,我可以在通过代码编辑器运行我的脚本时看到结果。我尝试在 onOpen() 或 onEdit() 中使用此脚本,以便在打开电子表格时能够获取数据。 但使用电子表格我收到了这条消息:
GoogleJsonResponseException: API call to bigquery.tables.list failed with error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
这是我放在 onOpen 函数中的代码:
function onOpen(){
let ui = SpreadsheetApp.getUi();
let method_list = ["SUM", "AVG", "COUNT"];
//Adding a Custom menu
ui.createMenu("Media Budget")
.addItem("Facebook", "makeQuery")
.addSeparator()
.addItem("Google Ads", "makeQuery")
.addToUi();
//Getting all datasets from the specified project
let dataset_list = getAllDataSets("dw_sandbox_");
Browser.msgBox(dataset_list);
//Creating dropdown list cell
let cell = SHEET.getRange("B1");
applyValidationToCell(dataset_list, cell);
}
除此之外,如果我尝试使用电子表格中的自定义菜单执行该功能,一切正常。
如果您能帮助我,将不胜感激。
【问题讨论】:
标签: api google-apps-script google-sheets google-bigquery