how to compare a variable to entries inside an Array - actionscript

Here is my problem.
I'm doing a little weather web app in flash.
So i read an xml feed and copose an array of data from it.
my xml is like this
<xml>
<prevision>
<date>22</date>
<hour>5</hour>
<temperature>40</temperature>
</prevision>
<prevision>
<date>22</date>
<hour>10</hour>
<temperature>44</temperature>
</prevision>
<prevision>
<date>22</date>
<hour>14</hour>
<temperature>45</temperature>
</prevision>
<prevision>
<date>22</date>
<hour>20</hour>
<temperature>37</temperature>
</prevision>
</xml>
So here is what i'm doing with my Actionscript 2
//**************************************
// Here i'm getting the current hour
var mytime = new Date();
var currenthour = mytime.getHours();
//*************************************
// Here i'm getting my XML
var myhour:Array = new Array();
var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function(success)
{
dataextractor = myxml.firstChild.childNodes;
for (var j = 0; j < dataextractor.length; j++)
{
myhour.push(dataextractor[j].childNodes[1].firstChild.nodeValue);
}
// Doing this
trace(mydate);
}
// Will return this
// 5 , 10, 14 , 20
Now what i would like to do is to find and trace the entry in mydate array which have a a value that approach the most to "currenthour" variable (which is my current time i'm getting upper).
For exemple if currenthour = 11
In the case that i have myhour array equal to 5 , 10 , 14 , 20
The best match will be myhour[1]
Can someone help to do so?
myhour Array don't have a fixed number of entries.
Sometime it can be 5, 10, 14 and other time just 14,20 etc
Thanks a lot,

I'm not sure, if I got your idea. Do you need to find the nearest number to currenthour from myhour array?
That's it:
var minDiff : int = 12;
var diff : int;
var index : int = 0;
for (var i : int = 0; i < myhour.length; i++)
{
diff = Math.abs(myhour[i] - currenthour);
if (diff > 12)
diff = 24 - diff;
if (diff < minDiff)
{
minDiff = diff;
index = i;
}
}
myhour[index] is the result. Hope, I got an idea.

Imagine your numbers on a line, from 0 to 24.
Using a variable to keep track of the smallest distance between the current hours and each hour in your XML, you can find the index of the node you're looking for:
import mx.xpath.XPathAPI;
var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function(loaded:Boolean) {
if (loaded) {
var hoursMin:Number = 24;
var hoursNow:Number = new Date().getUTCHours();
var hourID:Number;
var hours:Array = XPathAPI.selectNodeList(this.firstChild, "xml/prevision/hour");
var hoursNum:Number = hours.length;
for(var i:Number = 0 ; i < hoursNum; i++){
var hoursDiff:Number = Math.abs(hoursNow - parseInt(hours[i].firstChild));//look for the 'shortest distance' within 24 numbers
if(hoursDiff < hoursMin){//found the smallest current value
hoursMin = hoursDiff;//update the minimum
hourID = i;//store the node ID
}
}
trace(this.firstChild.childNodes[hourID]);//access the closest node in time
} else {
trace("XML Load Error!!");
}
}
myxml.load("feed.xml");
Here's the same thing without XPath, although I find accessing nodes a bit harder this way:
var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function(loaded:Boolean) {
if (loaded) {
var hoursMin:Number = 24;
var hoursNow:Number = new Date().getUTCHours();
var hourID:Number;
var hoursNum:Number = this.firstChild.childNodes.length;
for(var i:Number = 0 ; i < hoursNum; i++){
var hoursDiff:Number = Math.abs(hoursNow - parseInt(this.firstChild.childNodes[i].childNodes[1].firstChild));//look for the 'shortest distance' within 24 numbers
if(hoursDiff < hoursMin){//found the smallest current value
hoursMin = hoursDiff;//update the minimum
hourID = i;//store the node ID
}
}
trace(this.firstChild.childNodes[hourID]);//access the closest node in time
} else {
trace("XML Load Error!!");
}
}
myxml.load("feed.xml");
HTH

Related

How do i get the URL of a editable form response in spreadsheets

Im trying to get the url of a editable google form response to show up in google sheets,but it does not seem to be working.
I have seen Awesome Table and Ruben's example. Based of these 2 links and some others, they seem to be working for single form response sheets,but not multiple.
I tried this code 1st:
var formURL = 'https://docs.google.com/forms/d/__Your ID__/viewform';
var sheetName = '__Response sheet__';
var columnIndex = __column where it appears__;
function getEditResponseUrls() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = 2; i < data.length; i++) {
if (data[i][0] != '' && data[i][columnIndex-1] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if (formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue(editResponseUrl);
}
}
}
2nd is:
// Form URL
var formID = '__Your ID__';
// Sheet name used as destination of the form responses
var sheetName = '__Response sheet__'';
/*
* Name of the column to be used to hold the response edit URLs
* It should match exactly the header of the related column,
* otherwise it will do nothing.
*/
var columnName = '__name of column where it appears__' ;
// Responses starting row
var startRow = 2;
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var columnIndex = headers[0].indexOf(columnName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openById(formId);
for(var i = startRow-1; i < data.length; i++) {
if(data[i][0] && !data[i][columnIndex]) {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
}
}
}
Nothing is showing up, and when i check the logs for triggers, it is all working fine, no failures.I have tried putting the global variables within the function, but no changes.
The following points are taken from the latest version from #Rubén latest version on github. This code is a thing of beauty and a joy to behold. Combined with Rubén's detailed instructions, this answer can be setup and running in less than 5 minutes.
change
var formID = '__Your ID__';
to
var formURL = 'https://docs.google.com/forms/d/ -insert id - /edit';
you get the URL from the Forms Editor page.
change
var columnName = 'Form URL';
to
var sheetName = 'URL'; // Column U
replace getEditResponseUrls entirely
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var columnIndex = headers[0].indexOf(columnName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = startRow-1; i < data.length; i++) {
if(data[i][0] != '' && data[i][columnIndex] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
}
}
}
Remember to set the installable trigger.
In the Google Sheet that you have linked from the question above, you have declared two functions with the same name and this could be a reason why the first one may never be executing. I tested this code and it seemed to work.
You may want to replace everything in the file with just this code.
var id = '1SdSPhOwi1dWQzRsNpA9zFL-ODorgohST3TMRJLqz16I';
var sheetName = 'Order Information';
var columnIndex = 21;
function getEditResponseUrls() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openById(id);
for(var i = 2; i < data.length; i++) {
if (data[i][0] != '' && data[i][columnIndex-1] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if (formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue(editResponseUrl);
}
}
}

How to convert a OPENLAYERS 3 postcompose method to a normal one?

I am trying to animate a line based on the given coordinates array comprising of latitude and longitude and I want to call my function just once and my coordinates name is: replayData.
map.on('postcompose', function (event) {
var vectorContext = event.vectorContext;
var frameState = event.frameState;
vectorContext.setFillStrokeStyle(null, animatedLineStroke);
var features = lineVectorSource.getFeatures();
for (var k = 0; k < features.length; k++) {
var feature = features[k];
if (!feature.get('finished')) {
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new ol.geom.LineString(coords.slice(0, maxIndex));
if (feature.get('iValue') == undefined || feature.get('iValue') < k) {
// drawMovingCarMarker(coords, k);
feature.set('iValue', k)
}
vectorContext.drawLineStringGeometry(currentLine, feature);
}
}
frameState.animate = true;
});
What this function is doing is first collecting all the values from for loop and then starting the animation. And because of this if I've 5 points the line between first two points will be drawn 5 times then 4,3, and so on.
Any help would be entertained. Thanks in advance.

Google Spreadsheet ||Typeerror : cannot read property '0'

I have a spreadsheet for project data with time-sheet for each month logged against each project ID
I want to iterate through each sheet and if there is matching project ID , I want to sum up the number of hours logged for each project.
I have written the following code but keep getting the
TypeError: Cannot read property "0" from undefined. (line 31).
This is my sheet : https://goo.gl/rrsSxI
And this is my Code.
function TotalHours(TaskID) {
var a = SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sum = 0;
// var fcol = 0;
for (var i = 1; i <= a; ++i) {
// var sheetname = sheets[i].getName();
//var cell = sheets[i].getActiveCell();
//Set active cell to A1 on each sheet to start looking from there
SpreadsheetApp.setActiveSheet(sheets[i])
//var sheet = sh.getActiveSheet();
var range = sheets[i].getRange("A1");
//* sheets[i].setActiveRange(range);
var data = sheets[i].getDataRange().getValues();
for (var row = 2; row <= data.length; ++row) {
if (data[row][0] == TaskID) {
for (var col = 2; col <= 31; ++col) {
sum += sheets[i].getRange(row, col).getValue();
}
}
}
}
return sum;
}
Can someone help me with what I am doing wrong.
I assume you want to exclude the sheet where the formula is going to be used ("Tracker" ?
See if this works ?
function TotalHours(TaskID) {
var sum = 0,
s = SpreadsheetApp.getActive(),
active = s.getActiveSheet().getName(),
sheets = s.getSheets();
for (var i = 0, slen = sheets.length; i < slen; i++) {
if(sheets[i].getName() != active) {
var sheetVal = sheets[i].getDataRange()
.getValues();
for (var j = 0, vlen = sheetVal.length; j < vlen; j++) {
if (sheetVal[j][0] == TaskID) {
for (var k = 2, rlen = sheetVal[j].length; k < rlen; k++) {
var c = sheetVal[j][k]
sum += c && !isNaN(parseFloat(c)) && isFinite(c)? c : 0; //check if cell holds a number
}
}
}
}
}
return sum;
}

Google Script send email from a sheet in a specific date

follow script is used to get a date from a google sheet and if this date is equal to today or tomorrow generate an automatic email to my address in order to remind me.
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getActiveSheet();
sheet.setActiveRange(sheet.getRange("A1"));
var range = sheet.getDataRange(),
formulas = range.getValues();
var toDay = new Date();
for (var r=0; r<formulas.length; r++) {
for (var c=0; c<formulas[r].length; c++) {
//var value = sheet.getRange(r,c).getValue();
var value = range.getCell(r, c).getValue();
Logger.log(value);
if (value == "AAAA")
{
var index = r+2;
value = sheet.getRange(index,c).getValue();
while (value != "" || index >= formulas.length)
{
if (DateDiff.inDays(value,toDay)==1 || DateDiff.inDays(value,toDay)==0)
{
MailApp.sendEmail(myAdress,subject, text);
}
index = index + 3;
value = sheet.getRange(index,c).getValue();
}
}
}
}
}
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
when i run the script it fail in the line :
var value = range.getCell(r, c).getValue();
have you any suggestion in order to fix this bug?
thanks
Mario
try to change the line with:
var value = formulas[r][c]
it should also do less request to google spreadsheet and run faster.

How to Count and return the Values

I am facing a problem in the following script. I am not much into scripting at all and this is not my script also but here am getting the result which is grouping the values( For Example if i have a value A in three cells it should return the value as 3 instead it is returning AAA. Can someone help me out to count the values and return it
Thanks in Advance,
Here is the script :
function sumBackgroundColors(rangeString, color) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var sumRange = s.getRange(rangeString);
//var sum = 0;
var openCount = 0;
var sumRangeBackground = sumRange.getBackgroundColors();
var sumRangeValues = sumRange.getValues();
for(var row = 0; row < sumRangeBackground.length; row++ ) {
for(var col = 0; col < sumRangeBackground[0].length; col++ ) {
if( sumRangeValues[row][col]=="LG M"&& sumRangeBackground[row][col] == color ) {
openCount = openCount + sumRangeValues[row][col];
//if(sumRangeBackground[row][col] == color && sumRangeValues[row][col] == 1 ) {
// sum = sum + parseFloat(sumRangeValues[row][col]);
}
}
}
return openCount;
//return sum;
}
Here is a function which will take the value to be searched as argument. Rest of the things have been explained in comment lines.
function searchCount(value){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get the whole data from activesheet in a 2D array
var data = sheet.getDataRange().getValues();
//Initialize counter
var count = 0;
//Iterate through the array
for(var i in data){
for(var j in data[i]){
// if a match found, increament the counter
if(value.toString == data[i][j]){
count++;
}
}
}
// return the count value
return count;
}
your problem could be due to openCount = openCount + sumRangeValues[row][col];
According to your example sumRangeValues[row][col] isn't an int. int + not an int = ??? if you want to keep a count of things you probably want openCount++ to replace that line instead, which is just a shortcut to openCount = openCount + 1;
Even if sumRangeValues[row][col] was an int, that line still wouldn't be what you're looking for. If you're searching for all the 3s in your spreadsheet your code would find your first 3, and then that line would execute 0 = 0 + 3 congrats, you just found 3 threes. You would continue to add three every time you found a three.
Waqar's code is essentially your code (amazingly simplified, but it iterates the same way) except he doesn't check color and he uses ++ instead of that line.

Resources