Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Created January 11, 2023 00:01
Show Gist options
  • Save JayGoldberg/d9a22d7cd02b0c71caf4b7265ecb8811 to your computer and use it in GitHub Desktop.
Save JayGoldberg/d9a22d7cd02b0c71caf4b7265ecb8811 to your computer and use it in GitHub Desktop.
Create an external table
// create external table from CSV or Google Sheet, autodetect schema
function bqCreateCsvTable(csvDriveFileId) {
const CONFIG = setConfig();
let csvFile = DriveApp.getFileById(csvDriveFileId);
let tableId = csvFile.getName();
let bqConfig = {
"tableReference": {
"projectId": CONFIG.projectId,
"datasetId": CONFIG.bqDatasetId,
"tableId": tableId
},
"externalDataConfiguration": {
"autodetect": true,
"ignoreUnknownValues": false,
"sourceFormat": "CSV",
"sourceUris": [
`https://drive.google.com/open?id=${csvDriveFileId}`
],
"csvOptions": {
"fieldDelimiter": ",",
"allowQuotedNewlines": true,
"skipLeadingRows": "1"
}
}
}
BigQuery.Tables.insert(bqConfig, bqConfig.tableReference.projectId, bqConfig.tableReference.datasetId)
}
// TODO: for Google sheet
{
"tableReference": {
"projectId": projectId,
"datasetId": datasetId,
"tableId": tableId
},
"externalDataConfiguration": {
"autodetect": false,
"ignoreUnknownValues": false,
"sourceFormat": "GOOGLE_SHEETS",
"sourceUris": [
"https://docs.google.com/spreadsheets/d/1gKn4gff1gS-NbhMdjJ7WtdEidfg2M5vGR-MFHdhY-7q"
],
"googleSheetsOptions": {
"range": "Sheet1",
"skipLeadingRows": "0"
}
},
"schema": {
"fields": []
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment