Change stroke color in seleced group - javascript

I try to change stroke color for the group of the path on atrboard. I have document with banch of artboards and icon on each artboards. Each icon contains several groups of the figure. And I have to change that on each artboard. How can I do this with JavaScript in Adobe Illustrator? Thanks.
var doc = app.activeDocument;
var strokeColor = {"red":255, "green":0, "blue":0}
for (i = 0; i < doc.artboards.length; i++) {
doc.artboards.setActiveArtboardIndex(i);
doc.selectObjectsOnActiveArtboard();
var selectedObjects = doc.selection;
var numSelectedObjects = selectedObjects.length;
var docSelection = app.activeDocument.selection;
newRGBColor = new RGBColor ();
newRGBColor.red=strokeColor.red;
newRGBColor.green=strokeColor.green;
newRGBColor.blue=strokeColor.blue;
docSelection.strokeColor =newRGBColor
}

Try:
var doc = app.activeDocument;
var myStrokeColor = new RGBColor();
myStrokeColor.red = 255;
myStrokeColor.green = 0;
myStrokeColor.blue = 0;
for(var i = 0; i < doc.pathItems.length; i++){
doc.pathItems[i].strokeColor = myStrokeColor
}
You may want to take the time to read Adobe's documentation on scripting
To only change the stroke colour of selected items you can use:
var doc = app.activeDocument;
var newRGBColor = new RGBColor();
var mySelection = doc.selection;
newRGBColor.red = 255
newRGBColor.green = 0
newRGBColor.blue = 0
for(var i = 0; i < doc.mySelection; i++){
mySelection[i].strokeColor = newRGBColor
}

Related

How to code "re-run custom function" button in Google Sheet custom function?

I'm having the problem lots are having where my Apps Script code on Gsheets doesn't automatically update unless it registers a change in the parameters. The custom function I'm using counts the number of cells that are highlighted a specific color. Changing the color of a cell doesn't re-run the script. I wanted to add a check box in a cell and every time I click it, it reruns the function.
function COUNTCOLOR(countRange,colorRef,recalc) {
var activeRg = SpreadsheetApp.getActiveRange();
var activeSht = SpreadsheetApp.getActiveSheet();
var activeformula = activeRg.getFormula();
var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
var BackGround = activeSht.getRange(colorRefAddress).getBackground();
var countCells = 0;
for (var i = 0; i < backGrounds.length; i++)
for (var k = 0; k < backGrounds[i].length; k++)
if ( backGrounds[i][k] == BackGround )
countCells = countCells + 1;
return countCells;
};
The function works, but only without the "recalc" variable, which is the variable I need to add a check box to re-run the code. I get an error: "Range not found (line 6)". Any advice on getting this to work?
Thanks!
A simplier way
function COUNTCOLOR(countRange,recalc) {
var activeRg = SpreadsheetApp.getActiveRange();
var activeSht = SpreadsheetApp.getActiveSheet();
var activeformula = activeRg.getFormula();
var countRangeAddress = activeformula.match(/\((.*),/).pop().trim();
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var backGround = activeRg.getBackground();
var countCells = 0;
for (var i = 0; i < backGrounds.length; i++)
for (var k = 0; k < backGrounds[i].length; k++)
if ( backGrounds[i][k] == backGround )
countCells = countCells + 1;
return countCells;
};
The color reference is the backgrund of the cell that contains the formula.
It works for me this way:
function COUNTCOLOR(countRange,colorRef,recalc) {
var activeRg = SpreadsheetApp.getActiveRange();
var activeSht = SpreadsheetApp.getActiveSheet();
var activeformula = activeRg.getFormula();
var [countRangeAddress,colorRefAddress] = activeformula.match(/\((.*)\)/).pop().split(',');
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var BackGround = activeSht.getRange(colorRefAddress).getBackground();
var countCells = 0;
for (var i = 0; i < backGrounds.length; i++)
for (var k = 0; k < backGrounds[i].length; k++)
if ( backGrounds[i][k] == BackGround )
countCells = countCells + 1;
return countCells;
};
You can add trim() to every variable if you want.
It could be something like this:
var variables = activeformula.match(/\((.*)\)/).pop().split(',');
var countRangeAddress = variables[0].trim();
var colorRefAddress = variables[1].trim();
Or you can add replace() in the line to get rid of spaces:
var [countRangeAddress,colorRefAddress] = activeformula
.match(/\((.*)\)/).pop().replace(/\s+/g,'').split(',');

How to use the result of an IF statement as a variable

I have a code as follows:
function DetailFacture2() {
var ss = SpreadsheetApp.getActive();
var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
var lastrowpaste = FACTUREDevis.getLastRow();
var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
var data = DetailDEVIS.getDataRange().getValues();
var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == DetailD){ //[1] because column B
var firstrowcopy = i+1;
Logger.log(firstrowcopy)
return (firstrowcopy)
}
}
};
It does return the correct value, but how do you use "firstrowcopy" as a fixed var?
I would like to use as follows:
function DetailFacture2() {
var ss = SpreadsheetApp.getActive();
var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
var lastrowpaste = FACTUREDevis.getLastRow();
var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
var data = DetailDEVIS.getDataRange().getValues();
var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == DetailD){ //[1] because column B
var firstrowcopy = i+1;
var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
source.copyTo(destination);
}
}
};
But, as one would expect, it cannot work as it loops...
Not sure if I understand your question too. The code doesn't look well. Here is just my guess. Try to change the last lines this way:
// ...
var firstrowcopy = 0;
for (var i = 0; i < data.length; i++){
if(data[i][1] == DetailD){ //[1] because column B
firstrowcopy = i+1;
break;
}
}
var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
source.copyTo(destination);
}

JavaScript: How Can I Make A Var "Array" Work?

Is there a way to name a var using a sort of "Array?" My code is this:
for(var i = 0; i < (getHorizontalSquares * getVerticalSquares); i++){
var Square[i] = document.createElement("div");
Square[i].style.position = "relative";
Square[i].style.float = "left";
Square[i].style.width = "50px";
Square[i].style.height = "50px";
Square[i].id = "square" + (i + 1);
for(var ii = 0; ii < 6; ii++){
var TestColor = TestColorArray[Math.round(Math.random()*(TestColorArray.length - 1))];
getTestColor += TestColor;
}
Square[i].style.backgroundColor = "#" + getTestColor;
SquareCont.appendChild(Square[i]);
}
I know my code doesn't work, but I want to implement the same idea so I can get a result of this:
var Square1...
var Square2...
var Square3...
var Square4...
var Square5...
etc
I also tried doing a "Concentration" var, but it didn't work. How do I do this so the document doesn't append the same square multiple times?
var Square = {};
var SquareCont = document.createElement('div');
var getHorizontalSquares = 10;
var getVerticalSquares = 10;
var TestColorArray = ['a','b','c','f','e','0','1','2','3','3','4','5'];
var getTestColor = '';
for(var i = 0; i < (getHorizontalSquares * getVerticalSquares); i++){
Square['Square'+i] = document.createElement("div");
Square['Square'+i].style.position = "relative";
Square['Square'+i].style.float = "left";
Square['Square'+i].style.width = "50px";
Square['Square'+i].style.height = "50px";
Square['Square'+i].id = "square" + (i + 1);
for(var ii = 0; ii < 6; ii++){
var TestColor = TestColorArray[Math.round(Math.random()*(TestColorArray.length - 1))];
getTestColor += TestColor;
}
Square['Square'+i].style.backgroundColor = "#" + getTestColor;
SquareCont.appendChild(Square['Square'+i]);
getTestColor = '';
}
console.log(Square);
This example does what you want using an object instead of an array, but meets your desire to dynamically create accessible Square1, Square2, etc... They are all contained in Square. In the console with this snippet, you will see that 100 squares are created and added to the Square object. They will be accessible by Square.SquareX (where X is some number), or Square['SquareX'], or Square['Square'+X] where X is some number again.
Your declaration syntax is not valid. But, I think the larger point you are trying to get to is to be able to populate an array with dynamically created elements and that you can do:
var squares = []; // Array must exist before you can populate it
var testColorArray = ["green", "yellow", "blue", "orange", "silver"];
var getTestColor = null;
function makeSquares(count){
for(var i = 0; i < count; i++){
// Just create the element and configure it. No need to worry about the array yet
var element = document.createElement("div");
element.style.float = "left";
element.style.width = "75px";
element.style.height = "75px";
element.id = "square" + (i + 1);
element.style.backgroundColor = testColorArray[Math.floor(Math.random()* testColorArray.length)];
element.textContent = element.id;
squareCont.appendChild(element);
// Now, add the element to the arrray
squares.push(element);
}
// Test:
console.log(squares);
}
makeSquares(10);
<div id="squareCont"></div>

Adding duplicate tiles from different arrays PIXI.js

I'm trying to create a slot game;
I have some images that I put into an array
var createSlots = function(){
//setup images as tilingSprites
var slot1 = new PIXI.extras.TilingSprite(t1, 200, 200);
var slot2 = new PIXI.extras.TilingSprite(t2, 200, 200);
var slot3 = new PIXI.extras.TilingSprite(t3, 200, 200);
var slot4 = new PIXI.extras.TilingSprite(t4, 200, 200);
var slot5 = new PIXI.extras.TilingSprite(t5, 200, 200);
var slot6 = new PIXI.extras.TilingSprite(t6, 200, 200);
var slot7 = new PIXI.extras.TilingSprite(t7, 200, 200);
var slot8 = new PIXI.extras.TilingSprite(t8, 200, 200);
var slot9 = new PIXI.extras.TilingSprite(t9, 200, 200);
var slot10 = new PIXI.extras.TilingSprite(t10, 200, 200);
//push slots into array; images, sprites etc.
mainSlotArr.push(slot1, slot2, slot3, slot4, slot5, slot6, slot7, slot8, slot9, slot10);
};
for the moment I have 2 functions (will combine them once I get this working)
createReels1 and createReels2
what they do is copy the mainSlotArray use a shuffle function
Then populate to 2 columns (reels) each (at the moment createReels2 only does one reel)
it then removes the array element from the array it's using
The trouble I'm having is that whatever image tiles are used in createReels2, disappear if they are being used in createReels1 function, e.g if image1.png used in createReels2 and createReels1, then it is not visible in the first 2 reels
createReels functions below (alot of hard coding!)
var createReels1 = function(){
slotArr1 = mainSlotArr.slice();
shuffle(slotArr1);
var counter = 0;
var num = 0
for(var i = 0; i <2; i++){
var slotContainer = new PIXI.Container();
slotContainer.width = 100;
slotContainer.height = 400;
slotContainer.y = 100;
slotContainer.x = i*130;
stage.addChild(slotContainer);
slotContainerArr.push(slotContainer);
for(var j = 0; j < 3; j++){
var slot = slotArr1[j];
var toDel = slotArr1.indexOf(slot);
slot.scale.y = slot.scale.x = .5;
console.log(slot);
var nextY = j*(slot.height/2);
slot.y = nextY;
slotContainerArr[i].addChild(slot);
slotArr1.splice(toDel, 1);//remove from array
}
}
}
var createReels2 = function(){
slotArr2 = mainSlotArr.slice();
shuffle(slotArr2);
var counter = 0;
var num = 0
for(var i = 0; i <1; i++){
var slotContainer = new PIXI.Container();
slotContainer.width = 100;
slotContainer.height = 400;
slotContainer.y = 100;
slotContainer.x = 260;
stage.addChild(slotContainer);
slotContainerArr.push(slotContainer);
for(var j = 0; j < 3; j++){
var slot = slotArr2[j];
var toDel = slotArr2.indexOf(slot);
slot.scale.y = slot.scale.x = .5;
var nextY = j*(slot.height/2);
slot.y = nextY;
slotContainerArr[2].addChild(slot);
slotArr2.splice(toDel, 1);//remove from array
}
}
}
If I understood the code correctly, with quick check:
Sprites can have only one parent. If you check the Sprite object, it actually has a parent property. So slotArr1 and slotArr2 have identical Sprites and that fact doesn't change id you slice them. Then when you are assigning them to different containers, they get moved from one container to the other. You can reuse textures sure, but one Sprite can only have on parent.

If text in a cell contains a specific word then put an image in it

I hope someone can help me. How can I a put an Image in a td if there is a specific word in it? Here is my code:
var allTableCells = document.getElementsByTagName("td");
for (var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "ArmHW")
node.style.backgroundImage = "url('HW.png')"
P.S. Sorry for my english! And thank you!
Here is the complete code now... still not working =/
var allTableCells = document.getElementsByTagName("td");
var HW = new Image();
yourHW.src = "HW.png";
for (var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "#WebSecurity.CurrentUserName")
node.style.color = "Blue";
node.style.border = "solid"
}
for (var j = 0, max = allTableCells.length; j < max; i++) {
var node = allTableCells[j];
if (Text.toString().trim() === "ArmHW") {
node.removeChild(Text);
node.appendChild(yourHW);
}
Ok the last saved code is this one... Still doesn't work:
(Sorry for my english and lack of knowledge)
<script>
var allTableCells = document.getElementsByTagName("td");
for (var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "#WebSecurity.CurrentUserName")
node.style.color = "Blue";
node.style.border = "solid";
}
var allTableCells = document.getElementsByTagName("td");
for (var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "ArmHW")
node.style.backgroundImage = "url('HW.png')"
}
</script>
And here is a sample of the td we are talking about:
<table id="91" class="csmap">#foreach(var row in db.Query(selectCommand, Searcdb.Query(selectCommand, SearchTerm90)){<tr><td onmouseover="this.style.border='dashed'" onmouseout="this.style.border='solid'">#row.Owner<br>#row.Owner2</td></tr>}</Table>
Ok, it looks like all your code are right... Something in my page is causing disfunction... I can't put an image in the back of any td...
I'm trying to rewrite my page step by step and see where it stops. If you have any idea do not hesitate =D
Ok, something is missing. The code is working only if there is only one word in the td... My td's contain wordwordetc...
The script must find the good word in the td and show an image
var allTableCells = document.getElementsByTagName("td");
var yourImage = new Image();
yourImage.src = "HW.png";
for (var i = 0;i < allTableCells.length; i++) {
var node = allTableCells[i];
if (node.textContent.trim() === "ArmHW") {
node.textContent = "";
node.appendChild(yourImage);
}
}

Categories

Resources