我一直在尝试从包含Google表格数据的临时表(表定义)中进行查询,但是在code.gs中我一直遇到错误。下面是我的代码:
function test(){
var projectId = 'projectId';
var datasetId = 'datasetId';
var tableId = 'tableId';
var sheet_url = 'the_url_of_google_sheet';
var tableName = 'sheet_tab';
var tableProps = {
externalDataConfiguration:{
sourceUris: [sheet_url],
googleSheetOptions:{
skipLeadingRows: 1
},
sourceFormat: 'GOOGLE_SHEETS',
autoDetect: true
}
}
var tableDef = {};
tableDef[tableName] = tableProps;
var sql = 'SELECT * FROM ' +tableName+';';
var jobConfig = BigQuery.newJobConfigurationQuery();
jobConfig.destinationTable = {projectId : projectId, datasetId : datasetId,
tableId: tableId};
jobConfig.query = sql;
jobConfig.writeDisposition = 'WRITE_APPEND';
jobConfig.tableDefinitions = tableDef;
var queryR = BigQuery.Jobs.query(jobConfig, projectId)
}
无法解决错误消息“ TableName”,缺少数据集名称。
答案 0 :(得分:1)
您调用BigQuery时的错误源自您发送的无效SQL。您的SQL无效,因为您将字符串("SELECT * FROM"
)与Object
连接在一起。您需要将表标识符而不是表定义附加到SQL。 (您还缺少“ FROM”关键字和添加的内容之间的空格。)
从page you link开始,它给出了一个为临时表构造表定义的示例(在Python中):
# Configure the external data source and query job
external_config = bigquery.ExternalConfig('GOOGLE_SHEETS')
sheet_url = "some url"
external_config.source_uris = [sheet_url]
external_config.schema = [
bigquery.SchemaField('name', 'STRING'),
bigquery.SchemaField('post_abbr', 'STRING')
]
external_config.options.skip_leading_rows = 1 # optionally skip header row
table_id = 'us_states'
job_config = bigquery.QueryJobConfig()
job_config.table_definitions = {table_id: external_config} # <-- variable assignment in constructor
# Example query to find states starting with 'W'
sql = 'SELECT * FROM {} WHERE name like "W%"'.format(table_id)
query_job = client.query(sql, job_config=job_config) # API request
结论是,要在SQL中使用的表名是存储表定义的属性。在您的代码中,该名称为"tab"
:
var tabledef = {
tab: { // <--- the name of your temporary table is "tab"
sourceUris: [sheet_url],
googleSheetOptions:{
skipLeadingRows: 1
},
sourceFormat: 'GOOGLE_SHEETS',
autoDetect: true
}
}
请注意,Python代码可以在文字对象构造函数中为其dict
使用变量键分配,而not valid in Apps Script是相似的(“简写”)(但是可以在较新的JavaScript版本中完成) )。
向您和需要了解您的代码的任何人提供一种更明确的方式,可以为给定查询标识表:
const tableName = /** .... */;
const tableProps = {
...
};
const tableDef = {};
tableDef[tableName] = tableProps;
var query = "SELECT * FROM " + tableName + ";";
...