i wrote a code that should take a value from a cell and than convert it to the string and than into the array. its working fine. I can see the value of arr in Logs as an array with the input of the cell. For example, in the cell are " dog, cat " and tha arr value in Logs is [dog, cat].
But after i create this array, i would like to make a loop on it. And than i become in Logs, that arr is undefined. Can somebody help me please? im working on it for 2 days :(
Here is my code:
function animal (s,z){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange("SM");
var columnNumber = getColumnNumberOfSM(s);
var rowNumber = getRowNumberOfSM(z);
var colAction = columnNumber + 1;
var action = sheet.getRange(rowNumber, colAction, 1, 1).getValues();
var bar = action.toString();
var arr = [{}];
arr = bar.split(", ");
//return arr; // returns an array [dog, cat]
var foo = arr; // underfined
for (var i = 0; i <= foo.length; ++i) {
if (foo[i] == "dog") {
Logger.log(upload());
}
}
}
now i edit my code and its working fine,but only with "dog" but not with "cat"
function animal(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var action = sheet.getRange("C3").getValue();
var bar = action.toString();
var arr = bar.split(", ");
for (var i = 0; i <= arr.length; ++i) {
if (arr[i] == "dog") { // works
Logger.log(upload());
}
if (arr[i] == "cat") { // doesn't work
Logger.log(upload());
}
}
}
You don't need to initiate arr as blank you can directly initialize it as a result to split method , also one point I didn't get is why do you need to assign arr to foo? You can directly iterate through arr.length and perform the required operation.
Here's my working snippet.
var parts = path.split(",");
for (var i = 0; i < parts.length; i++) {
if (parts[i] == 'dog'){
Logger.log(parts[i]);
}
}
I've got this code that is looking for the closest number to 0 in my array :
var tideArray = new Array();
tideArray.push({tide:"haute", difference: "-14"});
tideArray.push({tide:"haute", difference: "3"});
tideArray.push({tide:"basse", difference: "-4"});
tideArray.push({tide:"basse", difference: "8"});
if (tideArray.length > 0)
{
var minItem: Object = tideArray[0];
for (var index:int = 1; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
}
}
}
trace(minItem.difference) // OUTPUT is 3 in this case
s there a way to find the index of minItem.difference in my tideArray ? (so, the result here should be index = 1 )
I've tried tideArray.indexOf(minItem.difference) but the output is -1, so the index wasn't found...
I'm looking for the index number and not the value of "difference" or "tide".
Try using map, e.g.:
tideArray.map(function (cv) { return cv.difference }).indexOf("-14")
A really simple approach would be to record the index using your existing loop:
if (tideArray.length > 0)
{
var mindex: int = NaN;
var minItem: Object = tideArray[0];
for (var index:int = 0; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
mindex = index;
}
}
trace("Min item index: " + mindex);
}
I'm trying to get some data from a Google Sheet to insert in two arrays with a for loop but I think I'm using the wrong methods.
This is the code:
function regValori() {
var datAgg = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("datiaggiornati");
var f5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio5");
var CValue = new Array();
var CName = new Array();
CValue.lenght = datAgg.getRange("B1").getValue();
CName.lenght = CValue.lenght;
for (var i = 0; i <= CValue.lenght; i++)
{
CValue[i] = datAgg.getCell(i, 16).getValue();
CName[i] = datAgg.getCell(i+2, 1).getValue();
}
var riga = f5.getRange("B2").getValue();
for (x = 3; x <= numCrypto; x++);
for (i=0;i<numCrypto;i++);
{
f5.getRange(riga, x).setValue(valCrypto[r+i]);
f5.getRange(2,numCrypto).setValue(nomiCrypto[r+i]);
}
}
The error:
TypeError: Impossible to find getCell function in the Sheet object
Thanks for your help!
Errors:
getCell() is a method of Range, not Sheet
Google Sheets starts from rowIndex = 1
If you don't need an offset (i.e. start from a certain cell), then you can use getRange:
for (var i = 0; i <= CValue.lenght; i++)
{
var r = i + 1; // rowIndex
CValue[i] = datAgg.getRange(r, 16).getValue();
CName[i] = datAgg.getRange(r+2, 1).getValue();
}
this is the solution:
function regValori() {
var datAgg = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("datiaggiornati");
var f5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio5");
var riga = f5.getRange(2, 2).getValue();
for (var i = 2; i <= 6; ++i) {
f5.getRange(riga, i + 1).setValue(datAgg.getRange("P" + i).getValue());
f5.getRange(2, i + 1).setValue(datAgg.getRange("A" + i).getValue());
}
}
i posted another question here for the same problem and I get a perfect answer how to use for loop to drastically reduce code lenght.
the only part I changed is this one: (var i = 2; i <= 6; ++i) with this one: (var i = 2; i <= n+1; ++i), so it can work perfectly with any variable n to get with command "var numCrypto = datAgg.getRange(1,2).getValue();"
In AS3 :
I've got a long text in an array.
var myHugeArray:Array = ["I love Apple
I have an Iphone
I eat a Banana
I'm John
I sell a computer
I sell an Apple
I love rock
I sell a car"];
How can I do to search a specifics words ? (like : show me sentences with the word "apple") --> output : "I love Apple" and "I sell an Apple"
Thanks,
EDIT
Here's what I did so far :
loader5.load(urlReq);
loader5.addEventListener(Event.COMPLETE,completeHandler2);
function completeHandler2(event:Event):void{
loader5.removeEventListener(Event.COMPLETE,completeHandler2);
trace("Données envoyées");
feedbackText.text = "Données envoyées";
loader5.load(urlReq);
loader5.addEventListener(Event.COMPLETE, complete);
}
function complete(e:Event):void {
addChild(list);
products = JSON.parse(loader5.data) as Array;
feedbackText.text = "complete";
for(var i:int = 0; i < products.length; i++){
createListItem(i, products[i]);
}
showList();
}
function createListItem(index:int, item:Object):void {
var listItem:TextField = new TextField();
listItem.text = item.title;
listItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
showDetails(item);
});
list.addChild(listItem);
str = item.title;
bar();
}
function bar(){
var arr: Array ;
searchBar.type = TextFieldType.INPUT;
var suggested:Array = new Array();
var textfields:Array = new Array();
searchBar.addEventListener(Event.CHANGE, suggest);
arr = str.split(",");
trace(arr);
function suggest(e:Event):void
{
suggested = [];
for (var i:int = 0; i < textfields.length; i++)
{
removeChild(textfields[i]);
}
textfields = [];
for (var j:int = 0; j < arr.length; j++)
{
if (arr[j].indexOf(searchBar.text.toLowerCase()) != -1)
{
var term:TextField = new TextField();
term.width = 360;
term.height = 24;
term.x = 18;
term.y = (24 * suggested.length) + 135;
term.border = true;
term.borderColor = 0x353535;
term.background = true;
term.backgroundColor = 0xFF9900;
term.textColor = 0x4C311D;
term.defaultTextFormat = format;
addChild(term);
suggested.push(arr[j]);
term.text = arr[j];
}
}
function showList():void {
list.visible = true;
}
function showDetails(item:Object):void {
titleTxt.htmlText = item.title;
detailsTxt.htmlText = "<U>prix:</U> " + item.prix + " xpf"+ "\n\n<U>Description:</U> " + "\n"+item.theDescription + "\n"+"\n\n<U>Contact:</U> " + item.mail+ "\n"+item.phone;
}
So, my AS3 code go search for PHP variable with loader5.
All the items found by the php are put in an Array (products).
And a list of all the products is created. (createListItem).
If I click on an item, it show me some details (price, description..etc). It's the function showDetails();
Know I've created a searchBar (autocomplete).
An array is created (arr) that split the string (str).
Then it does what it does to search through the array.
Problems :
1/ Weirdly, not all the words are displayed in my searchBar. Some words are working, other not.
2/ How can I do to call the function showDetails() when the user click on the suggest term ? (term.addEventListener(MouseEvent.CLICK, showDetails)); doesn't work as the terms is not item.title. ShowDetails is showing details of item.title. (so how can I say that term = item.title ?)
3/ Do you see a way simpler than that ?
Your myHugeArray is just string, so split() it with \n', you got the
ret array for example, then find the one contains the word you search, like "apple", using indexof() in each string
You need to split the string into an array then search each item
check this out
https://stackoverflow.com/a/34842518/3623547
(I am working with Angular but this is not an angular specific problem).I have a json object that i am trying to disect. I've been trying to get my head around this and have come up with a complicated script with nested for loops that isn't working. The object consists of 3 arrays. The first array is an array of fieldnames. The third array is an array of arrays that correspond to the field names like so:
object = {"fields" : [array], "type" : [array], "values" : [array]}
where:
fields = ["user","bananas","pies","apples","pears","mangos","date"]
values = [["Bongo","12","2","1","2","4","05-02-2015"], ["Mongo","12","23","15","22","43","05-02-2015"], ["Congo","15","32","21","23","44","06-02-2015"]]
What i want to do is count the amount of fruit that all users had on a specific date. for instance i want to know how many bananas all users had on "06-02-2015".
I can post my code, but i think that would only work confusing as it's probably wrong and redundant.
update:
I've tried the filter but can't get it to work because the values inside object.values don't have a name. So i think it should be something like this:
var fruits = $filter('filter')(object.value, {???[6] : "2015-04-29 00:00:00"}, true);
I'm not sure what the ??? should be.
solved:
simply removing the name did the trick.
var fruits = $filter('filter')(object.value, "2015-04-29 00:00:00", true);
This script should group the values by date:
var fields = ["user","bananas","pies","apples","pears","mangos","date"];
var values = [["Bongo","12","2","1","2","4","05-02-2015"], ["Mongo","12","23","15","22","43","05-02-2015"], ["Congo","15","32","21","23","44","06-02-2015"]];
var result = {};
for (var j = 0; j < values.length; j++) {
if (!result.hasOwnProperty(values[j][fields.length - 1]))
result[values[j][fields.length - 1]] = {};
for (var i = 1; i < fields.length - 1; i++) {
if (!result[values[j][fields.length - 1]][fields[i]])
result[values[j][fields.length - 1]][fields[i]] = parseInt(values[j][i], 10);
else
result[values[j][fields.length - 1]][fields[i]] += parseInt(values[j][i], 10);
}
}
console.log(result);
What you need is a filter:
var fruits = $filter('filter')(object, {date: $choosenDate}, true);
Then, if you want to split fruit by type, you can use angular.forEach to iterate on the nev fruits var
This would be a lot simpler if the structure of the arrays wasn't bananas, so to speak.
var fields = ["user","bananas","pies","apples","pears","mangos","date"];
var values = [["Bongo","12","2","1","2","4","05-02-2015"], ["Mongo","12","23","15","22","43","05-02-2015"], ["Congo","15","32","21","23","44","06-02-2015"]];
var date, dataArray, dateIndex = (fields.length - 1),
dates = {};
for (var i = 0; i < values.length; i++) {
dataArray = values[i];
date = dataArray[dateIndex];
dates[date] = dates[date] || {};
for (j = 1; j < fields.length - 1; j++) {
dates[date][fields[j]] = dates[date][fields[j]] || 0;
dates[date][fields[j]] += Number(dataArray[j]);
}
}
console.log(dates);