Last active
April 7, 2019 21:54
-
-
Save amitrahav/f897361f2250134b9072f14777d5525e to your computer and use it in GitHub Desktop.
googleAppScript for updating sheets at giveback (by headstart) donations data
This file contains 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
var ss = SpreadsheetApp.getActiveSheet(); | |
var lastRow = ss.getLastRow(); | |
// my table col where: timestemp, amount, diff, donorsNum, donors name | |
var MONEYCOL = 2; | |
var DonorsCOL = 4; | |
function parseHtml(searchString){ | |
var url = ''; //this is the roject URL. tested on www.giveback.co.il, should probebly work on https://headstart.co.il/ with minor changes | |
var html = UrlFetchApp.fetch(url).getContentText(); | |
if (html.search(searchString) >= 0) { | |
return html.split(searchString)[1]; | |
} | |
} | |
function parseHtmlCountDonationAndMoney() { | |
var string1 = parseHtml('projectstatus">'); //after projectstatus div | |
var string2 = string1.split('<div class="cu">')[1]; // after counter | |
var string3 = string2.split('<b>')[0]; // before ש"ח b | |
var trimed = string3.replace(/\s/g, ""); | |
var donorsString2 = string1.split('<div class="t1">')[1]; | |
var donorsString3 = donorsString2.split('</div>')[0]; | |
var donorsTrimed = donorsString3.replace(/\s/g, ""); | |
var currentMount = trimed.replace(",", ""); | |
var currentDonors = donorsTrimed.replace(",", ""); | |
return {currentMount:currentMount,currentDonors:currentDonors}; | |
} | |
function newDonorsNumber(currentDonors) { | |
var donerRange = ss.getRange(lastRow, DonorsCOL); | |
return currentDonors - donerRange.getValue(); | |
} | |
function parseHtmlDonerNames(numOfLastDonorsNames) { | |
var string1 = parseHtml('descclass_supp">'); // after projectstatus div | |
var Donors = string1.split('<div class="le_sp">'); // each doner | |
var donerNames = []; | |
for each(var doner in Donors){ | |
if (doner.search('</b>') >= 0) { | |
var name = doner.split('</b>')[0]; | |
var trimedName = name.replace("<b>","").replace("\n","").split(" "); | |
donerNames.push(trimedName[trimedName.length-1]); | |
} | |
} | |
return donerNames.slice(0,numOfLastDonorsNames).join(','); | |
} | |
function prevSum(){ | |
var range = ss.getRange(lastRow, MONEYCOL); | |
return range.getValue(); | |
} | |
function insertNewRow(parsedHtml,prevousSum,donorsName){ | |
var nextRow = lastRow + 1; | |
var range = ss.getRange(nextRow, 1, 1, 5); | |
var formattedDate = Utilities.formatDate(new Date(), "GMT+2:00", "dd-MM-yyyy HH:mm:ss"); | |
range | |
.setValues([ | |
[formattedDate, parseInt(parsedHtml.currentMount, 0) , parseInt(parsedHtml.currentMount - prevousSum, 0), parsedHtml.currentDonors, donorsName] | |
]); | |
return; | |
} | |
function handle(){ | |
var parsedHtmlCounters = parseHtmlCountDonationAndMoney(); | |
var prevousSum = prevSum(); | |
if(parsedHtmlCounters.currentMount != prevousSum){ | |
var donorNum = newDonorsNumber(parsedHtmlCounters.currentDonors); | |
var parsedHtmlDonors = parseHtmlDonerNames(donorNum); | |
insertNewRow(parsedHtmlCounters,prevousSum, parsedHtmlDonors); | |
Logger.log('sum is updated to ' + parsedHtmlCounters.currentMount +' - yaii'); | |
}else{ | |
Logger.log(parsedHtmlCounters.currentMount + ' is the same as before'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment