Created
January 11, 2023 00:01
-
-
Save JayGoldberg/d9a22d7cd02b0c71caf4b7265ecb8811 to your computer and use it in GitHub Desktop.
Create an external table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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