I'm using bigquery API from the Google App script and running the queries in a loop and the code is running fine. At the end of the code I'm fetching few stats of the query and writing it into the spreadsheet.
While we see the sheet, few of the cell are filled with undefined
. Could someone help me here?
Here is the SS enter image description here
Also if i'm running more than 15 queries its throwing me an error saying execution time exceeded
.
Here is the code:
function finalqueries() {
var projectId = 'xxxxxx';
var queries = [];
var totalbytes = [];
var timelapsed = [];
var rowcount = [];
var finalSheet = SpreadsheetApp.getActive().getSheetByName('Final Metrics');
queries = finalSheet.getRange(2, 1, finalSheet.getLastRow()-1).getValues();
// var userProperties = PropertiesService.getUserProperties();
// var loopCounter = Number(userProperties.getProperty('loopCounter'));
// var limit = queries.length;
for(var i=1; i<=10; i++) {
var sqlstmt = queries[i-1];
try{
var request2 = {
query: sqlstmt,
useLegacySql: true
};
var queryResults2 = BigQuery.Jobs.query(request2, projectId);
var jobId1 = queryResults2.jobReference.jobId;
// timer:
// while (!queryResults2.jobComplete) {
// Utilities.sleep(sleepTimeMs);
// sleepTimeMs *= 2;
// queryResults2 = BigQuery.Jobs.getQueryResults(projectId, jobId1);
// if(sleepTimeMs>120000){
// break timer;
// }
// }
var queryDetails = BigQuery.Jobs.get(projectId, jobId1);
totalbytes.push(queryDetails.statistics.totalBytesProcessed);
timelapsed.push(queryDetails.statistics.totalSlotMs);
rowcount.push(queryResults2.totalRows);
}
catch(e){
try{
var request2 = {
query: sqlstmt,
useLegacySql: false
};
var queryResults2 = BigQuery.Jobs.query(request2, projectId);
var jobId1 = queryResults2.jobReference.jobId;
var sleepTimeMs = 5000;
// timer:
// while(!queryResults2.jobComplete) {
// Utilities.sleep(sleepTimeMs);
// sleepTimeMs *= 2;
// queryResults2 = BigQuery.Jobs.getQueryResults(projectId, jobId1);
// if(sleepTimeMs>120000){
// break timer;
// }
// }
var queryDetails = BigQuery.Jobs.get(projectId, jobId1);
totalbytes.push(queryDetails.statistics.totalBytesProcessed);
timelapsed.push(queryDetails.statistics.totalSlotMs);
rowcount.push(queryResults2.totalRows);
}
catch(e){
Logger.log(e);
}
Logger.log(e);
}
finalSheet.getRange(i+1, 2).setValue(totalbytes[i-1]);
finalSheet.getRange(i+1, 3).setValue(timelapsed[i-1]);
// finalSheet.getRange(i+1, 2).setValue((totalbytes[i-1]/1048576).toFixed(2) + 'MB');
// finalSheet.getRange(i+1, 3).setValue((timelapsed[i-1]/1000).toFixed(2) + 's');
finalSheet.getRange(i+1, 4).setValue(rowcount[i-1]);
}
}