Impossible to find the function setBackground on a object - arrays

I'm trying to do something like: If the column E has the word ok, column D gets formated as red. Seens preety easy, but something is wrong as I'm not entenring the if. Do u guys know what is wrong? I have read some other topics, but seens preety much the same
function onEdit() {
var ss =SpreadsheetApp.getActiveSheet();
var myRangeValues = ss.getRange('D7:E').getValues();
Browser.msgBox(myRangeValues);
for (var i = 0; i < myRangeValues.length; i++){
if(myRangeValues[i][0] == 'ok'){
myRangeValues[i][0].setBackground('red');
}
}
}
Thanks for the attention.
EDIT:
new code not working. I tried to put the logger.log in other lines as well. even without the logger, the error I get is that the function setBackground is impossible to find in the object.
function onEdit() {
var ss =SpreadsheetApp.getActiveSheet();
var myRangeValues = ss.getRange('D7:E').getValues();
for (var i = 0; i < myRangeValues.length; i++)
{
if(myRangeValues[i][1] == 'ok')
{
myRangeValues[i][0].setBackground('red');
//Logger.log("myRangeValues[i][1]: " + myRangeValues[i][1]);
}
}
}

You are mixing up methods.
value arrays don't have setBackground() method, this is a spreadsheet range method
use the code below to do what you want :
function onEdit() {
var ss =SpreadsheetApp.getActiveSheet();
var myRangeValues = ss.getRange('D7:E').getValues();
var myRangeColors = ss.getRange('D7:E').getBackgrounds();// get the colors
for (var i = 0; i < myRangeValues.length; i++)
{
if(myRangeValues[i][1] == 'ok')
{
myRangeColors[i][0]='#F00';
}
}
ss.getRange('D7:E').setBackgrounds(myRangeColors); //set the modified colors
}

Related

AngularJS - Delete check box is not working correctly and only deleting one at a time

I've written out a block of code that allows the user to check or uncheck entities that will be added or removed via web services. My add function seems to be working correctly and provides the ability to add multiple entities. However, my delete function isn't working the same. It doesn't delete each time, and can only delete one at a time. I'm struggling since the code is effectively the same as the add, so I don't know if the issue is AngularJS related or perhaps my web service isn't working correctly.
Edit: I've actually noticed that the for loop goes through it all but doesn't select the correct id, it always starts from the first one.
var toDeleteService = [];
for (var i = 0; i < $scope.siteServices.length; i++) {
if ($scope.siteServices[i].chosen != $scope.siteServices[i].origChosen) {
if ($scope.siteServices[i].chosen == true) {
toAddService.push(i);
}
else {
toDeleteService.push(i);
}
}
}
if (toDeleteService.length > 0) {
var deleteRequest = {};
deleteRequest.services = [];
for (var i = 0; i < toDeleteService.length; i++) {
var parentServiceName = $scope.siteServices[i].parentServiceName;
var j = 0;
for (; j < deleteRequest.services.length; j++) {
if (deleteRequest.services[j].parentServiceName == parentServiceName) {
break;
}
}
if (j == deleteRequest.services.length) {
deleteRequest.services[j] = {};
deleteRequest.services[j].parentServiceName = parentServiceName;
deleteRequest.services[j].subservices = [];
}
var service = {};
service.serviceId = $scope.siteServices[i].serviceId;
deleteRequest.services[j].subservices.push(service);
}
var deleteUrl = "api/sites/" + $scope.targetEntity.siteId + "/services/" + service.serviceId;
$http.delete(deleteUrl)
.then(function (response) {
});
}
As I understood it you are trying to remove siteServices based by numbers stored in var toDeleteServices = [] so you need to access those numbers by its index. but in service.serviceId = $scope.siteServices[i].serviceId; you are using i instead.
service.serviceId = $scope.siteServices[toDeleteServices[i]].serviceId; as you need actual number of the service to delete.
If I understood your code correctly.

Splice array within array

How can I splice an array within another array?
I'm trying to create a game for kids in my class. Some kind of a trivia history question creator. How to splice the MasterArray so that I would get rid of sub-arrays (Hard-England and Medium-England) within the allEnglandArray. Because the "MasterArray.splice()" seem to be affecting - splicing only the allEngland array, or the allFrance array. But I need to get rid of those sub-arrays...
My code:
var Easy-England:Array = ["item1","item2","item3","item4","item5"];
var Medium-England:Array = ["item6","item7","item8"];
var Hard-England:Array = ["item9","item10"];
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
var Easy-France:Array = ["item11","item12","item13","item14","item15"];
var Medium-France:Array = ["item16","item17","item18"];
var Hard-France:Array = ["item19","item20"];
var allFrance:Array = [Easy-France,Medium-France,Hard-France];
// the list of countries goes on and on and on... (Italy, Hungary, etc.)
var allStuff:Array = [allEngland, allFrance, etc.];
var MasterArray:Array;
// FUNCTIONS
// clear MasterArray - first I clear out completely the MasterArray
function clearMasterArray():void
{
MasterArray.splice(0);
}
// update MasterArray - than I fill the MasterArray with data according to checkBoxes
function updateMasterArray():void
{
for (var i:int = 0; i<checkBoxBlock.myCheckBoxes.length; i++)
{
if (checkBoxBlock.myCheckBoxes[i].selected)
{
MasterArray.push(allStuff[i]);
}
}
}
// splice MasterArray - last thing I do is splice the items according to student's proficiency level, referred to as "studentPL".
function spliceMasterArray():void
{
if (studentPL == 1)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(5,5);
}
}
if (studentPL == 2)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(8,2);
}
}
if (studentPL == 3)
{
for (var i:int = 0; i<allStuff.length; i++)
{
trace("no need to splice");
}
}
}
And after this I call those functions in another function in this order...
function creatorFunction():void
{
clearMasterArray();
updateMasterArray();
spliceMasterArray();
}
Instead of:
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
try this:
var allEngland:Array = Easy-England.concat(Medium-England, Hard-England);
This way you will have a 'flat' array (no sub-arrays), so it will be easier to deal with.

The first element of an Object Array is passed as undefined when using JSON.parse

I'm using HTML5's local storage to save a small database of user's preferences. To do that I have the following function:
function save(UserDB) {
if (window.localStorage) { // Only do this if the browser supports it
localStorage.setObject('UserDataBase', JSON.stringify(UserDB));
}
with
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
UserDB is an array of objects similar to this:
[{"uid":"ABC","groupname":"My Group","calendarid":"sdf44d7g3ak5q8ifdrl308hk0#group.calendar.google.com","link":null,"userenable":true,"color":"528800"},{"uid":"CdO","groupname":"CKHO","calendarid":"apkrty45sdfer44fd1mr55dfghfg8#group.calendar.google.com","link":null,"userenable":true,"color":"AB8B00"}]
This seems to work just fine.
Then, when the user loads the site again I'd want to be able to regenerate the object array in a variable from the information stored in the previous session:
Storage.prototype.getObject = function(key) {
var value = JSON.parse(this.getItem(key));
return JSON.parse(value);
}
This also seems to work. The problem is when I use this last function to actually store the array:
function ApplyUserConfiguration(Data) {
if (window.localStorage) {
var UserDBx = localStorage.getObject('UserDataBase');
console.log("User Configuraiton found");
console.log(UserDBx);
console.log("ActualGetObject");
console.log(localStorage.getObject('UserDataBase'));
console.log(UserDBx.length);
for (var i = 0; i < Data.length; i++){
for (var j=0; j< 5; j++){
if(Data[i].uid == UserDBx[j].uid){
Data[i].userenable = UserDBx[j].userenable;
Data[i].Color = UserDBx[j].color;
delete UserDBx[j];
break;
}
}
}
}
return Data;
};
It turned out that console.log(localStorage.getObject('UserDataBase')); returns the object array correctly but console.log(UserDBx); returns the object array with the first element as "undefined". Any idea why this happens?
I faced the same issue of seeing undefined element in start. Following was my code:
var x;
for (i = 0; i < content.length; i++) {
x += content[i].name;
console.log(x);
}
The reason was undefined x. I was declaring it but I was not assigning it any value. So I just changed var x to var x="" and it solved the problem.
Error free code
var x="";
for (i = 0; i < content.length; i++) {
x += content[i].name;
console.log(x);
}

Remove Array and its contents with mouse click AS3 Flash CS5.5

I have been trying to do this for two nights now and haven't had any joy, please help if you can...
Simple throwing game with darts and other weapons, what I am trying to do is
Remove an array and all of its Children when I change weapon,
I feel sure there is a simple snippet of code that will remove all the children and array with no hassle but I haven't yet figured it out, if you know or could suggest anything, I would really appreciate it.
something like "removeArrayAndAllInstances(balls);" if only...
at the moment I have....(balls is the array in question)
for(var inter:int = balls.length - 1; inter > -1; inter--)
{
balls.splice(1);
balls.splice(1, balls.length);
}
but this docent work for some reason, the array and all of its children are all still on the stage.
I also tried
balls[];
No luck...
Please don't judge my code I am a novice as I am sure was evident and I know its a disgusting mess, sorry (Its the only way it makes sense to me).
I have tried numerous things, hope someone can help
Thanks in advance.....
var mouseTarget:MovieClip;
var balls:Array = new Array();
var ball:MovieClip = new dart();
var hammers:MovieClip = new hammer();
ball.x = 150;
ball.y = 50;
hammer_btn.addEventListener(MouseEvent.MOUSE_DOWN, hammerweapon);
dart_btn.addEventListener(MouseEvent.MOUSE_DOWN, dartweapon);
function removealldartsfromstage(e:MouseEvent):void
{
for(var inter:int = balls.length - 1; inter > -1; inter--)
{
balls.splice(1);
balls.splice(1, balls.length);
}
stage.removeEventListener(MouseEvent.MOUSE_UP, addDart);
}
function dartweapon(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, addHammer);
dart_btn.removeEventListener(MouseEvent.CLICK, dartweapon);
stage.addEventListener(MouseEvent.MOUSE_UP, addDart);
//removeChild(balls);
}
function hammerweapon(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, addDart);
dart_btn.addEventListener(MouseEvent.MOUSE_DOWN, dartweapon);
//stage.addEventListener(MouseEvent.MOUSE_UP, addDart);
stage.addEventListener(MouseEvent.MOUSE_UP, addHammer);
}
function addHammer(e:MouseEvent):void
{
var hammers = new hammer();
addChild(hammers);
removeChild(dart_btn);
addChild(dart_btn);
dart_btn.addEventListener(MouseEvent.CLICK, dartweapon);
balls.splice(10);
}
function addDart(e:MouseEvent):void
{
str.alpha = 0;
var ball = new dart();
addChild(ball);
removeChild(hammer_btn);
addChild(hammer_btn);
ball.x = 150;
ball.y = 50;
balls.push(ball);
trace(balls);
addEventListener(Event.ENTER_FRAME, checkIfHitTest);
hammer_btn.addEventListener(MouseEvent.MOUSE_DOWN, removealldartsfromstage);
function checkIfHitTest(Event)
{
for (var i:int = 0; i<balls.length; i++)
{
if (balls[i].dart_point.hitTestObject(eyeleft))
{
trace("hitleftbullseye");
ball.gotoAndStop("hitlefteyeframe");
Event.currentTarget.removeEventListener(Event.type, checkIfHitTest);
balls.splice(i, 1);
}
}
}
}
The balls array is just a storage for ball references. It bears no relation to the stage or the DisplayObjectContainer which they have been added at all. So you have to remove them individually.
This is what I'd do:
while(balls.length > 0)
{
var ball:MovieClip = balls.pop();
if (ball.parent) // Just to make sure you are referencing the correct container.
{
ball.parent.removeChild(ball);
}
}
I can't follow the logic of your game very well. That said, you'd do well to create conatiners for separate group of clips in order to make managing them easier. For example, in the creation stage:
var ballContainer:Sprite = new Sprite();
addChild(ballContainer);
for (var i:int = 0; i < ballLimit; i++)
{
var ball:Dart = new Dart();
ballContainer.addChild(ball);
}
This way, you can clear them of children all at once:
function removeAllChildren(container:Sprite) // Or just DisplayObjectContainer
{
while(container.numChildren > 0)
{
container.removeChild(container.getChildAt(0));
}
}

Randomly removing an array

just for the record, i'm using AS3.
I have an issue where I would like to remove a sprite randomly in AS3, I have managed to figure out how to create the sprites so that they fill as a grid, just for the life of me I can't figure out how to remove them!
Here's the code i've used to create them:
function showpixels() : void
{
for (var i:int = 0; i < 40; i++)
{
for (var j:int = 0; j < 40; j++)
{
var s:Sprite = new Sprite();
s.graphics.beginFill(0);
s.graphics.drawRect(i*10, j*10, 10, 10);
s.graphics.endFill();
addChild(s);
pixels.push(s);
}
}
}
Basically I need these to be removed randomly until what's underneath can be seen.
Any help would be good, I'm pretty new to this! Thanks!
function removeRandom():void
{
var rand:uint = Math.random()*pixels.length;
var i:Sprite = Sprite(pixels[rand]);
if(i.parent) i.parent.removeChild(i);
pixels.splice(rand, 1);
}
UPDATE: To remove at random intervals you could try something like this:
var _timer:int = 100;
addEventListener(Event.ENTER_FRAME, _handle);
function _handle(e:Event):void
{
if(pixels.length > 0) _timer --;
if(_timer < 1)
{
_timer = 10 + Math.random()*50;
removeRandom();
}
}
function removeRandom():void
{
var rand:uint = Math.random()*pixels.length;
var i:Sprite = Sprite(pixels[rand]);
if(i.parent) i.parent.removeChild(i);
pixels.splice(rand, 1);
}
Marty's idea works. Another option would be to shuffle the array first and then just pop off elements.
To shuffle an Array use pixels.sort(function (...args):int { return int(2*Math.random()-1) }).
And then you can simple remove them like this:
function remove():void {
if (pixels.length) removeChild(pixels.pop());
else clearInterval(this.id);
}
And add this line at the end of showpixels:
this.id = setInterval(remove, 500);

Resources