Skip to content

Instantly share code, notes, and snippets.

@sudevschiz
Created March 26, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudevschiz/3061041526867ff2ef6f1947ef9716d0 to your computer and use it in GitHub Desktop.
Save sudevschiz/3061041526867ff2ef6f1947ef9716d0 to your computer and use it in GitHub Desktop.
In Google Scripts : Fetch data from an API, compare some change and update last updated time
function time_wise_update(){
var getSheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var setSheet = SpreadsheetApp.getActive().getSheetByName("Sheet2");
var url = 'https://xxx/data.json';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var data = JSON.parse(json);
var ss = data.ss;
// For each row, compare all the elements of the JSON
var last_row = getSheet.getLastRow();
for(var i = 2; i <= last_row; i = i+1){
var s = getSheet.getRange(i,1);
var active = getSheet.getRange(i,5);
var l_time = setSheet.getRange(i,6);
for(var j=0;j<ss.length;j++){
if(ss[j].state == s.getValue()){
console.log(ss[j].active,active.getValue(),(ss[j].active - active.getValue()))
if((ss[j].active - active.getValue()) != 0){
var now = Utilities.formatDate(new Date(), "GMT+05:30", "dd/MM/yyyy HH:mm:ss") ;
l_time.setValue(now);
console.log(ss[j].state,ss[j].active,s.getValue(),active.getValue(),now)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment