Reputation: 47
I'm trying to use the following Google script to add data from Redash, then add the current date at the end. Where could I add a var now = new Date();
to input the date on the last row?
The desired outcome is the following. A-C is the data I'm successfully already pulling from Redash, but I want to add the current date. The date will always be in F if that helps.
https://www.dropbox.com/s/lfu9jk06j0fduo8/Screenshot%202020-05-20%2018.44.26.png?dl=0
Thank you for your help!
//FUNCTION FOR GETTING DATA FROM REDASH QUERIES
function getapidata(response, sheetname) {
var array1 = response.getContentText(); //Store data from redash into this variable
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var iLen = array1.length;
var i = 0;
var stringvalue1 = "";
var s1 = "";
var num1 = 1;
var num2 = 1;
var noheader = 1;
for (i = 0; i < iLen; i++) {
var j = i;
do {
s1 = array1[j];
if (s1 == ",") {
if (noheader == 1)
noheader = 1;
else
sheet.getRange(sheet.getLastRow() + num1, num2).setValue(stringvalue1);
num1 = 0;
num2++;
stringvalue1 = "";
} else
stringvalue1 = stringvalue1 + s1;
j++;
} while (s1 != "\n")
if (noheader == 1)
noheader = 1;
else
sheet.getRange(sheet.getLastRow() + num1, num2).setValue(stringvalue1);
noheader = 2;
num1 = 1;
num2 = 1;
stringvalue1 = "";
i = j - 1;
}
}
function getfromRedash() {
//Current SHR (Max Preps)
var redashapi = "API"; //Storing Redash API Value.
var sheetname = "SHEETTITLE"; //Assign your sheetname(where you would want data to be imported) to this variable
var response = UrlFetchApp.fetch(redashapi, {
muteHttpExceptions: true
}); //Storing Redash Cached Query result in this variable.
Logger.log(response.getContentText()); //If any error, error to be logged
getapidata(response, sheetname); //Call function for writing data in google sheets
//The 5 lines of code above can be repeated for multiple sheets in a single google spreadsheet file. For. Eg. If you have 5 different sheets in a single google worksheet, you can just copy paste the above lines of code, and just change the variable “redashapi” so as to make calls to the appropriate redash queries.
}
//THIS FUNCTION IS TO MAKE A NEW MENU IN GOOGLE SHEETS
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('MENU')
.addItem('Get Data from Redash', 'getfromRedash')
.addToUi();
}
Upvotes: 0
Views: 1029
Reputation: 1580
Well, you row number addressing is a bit confusing, cause sheet.getLastRow() would change every time you add values after the last row, but you can write something like
sheet.getRange(sheet.getLastRow() + num1, 6).setValue(new Date());
to insert date. You can refer other answers, like
How to format a JavaScript date
for date formatting.
Upvotes: 1