For loop only running once in openFile function - javascript

I'm trying to use a function to read an external file and split segments of it into an array using a for loop. For some reason the for loop only occurs once and I cant figure out why for the life of me.
I've tried commenting out sections of the code to see if anything in particular is causing it but I cant seem to isolate it exactly
function nthIndex(str, pat, n){ //just used to put string of hsl values from txt file into cArray
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
function openFile(event){
var input = event.target;
var r = new FileReader();
r.readAsText(input.files[0]);
r.onload = function(){
var text = r.result;
var n = 3;
var index1 = 0;
var index2 = nthIndex(text,",",n)
for(c=0;c<361;c++){ //WHY DOESNT THIS LOOP
var value = text.slice(index1, indexOf(",",index2));
cArray.push(value);
index1 = indexOf(",",index2);
index2 = nthIndex(text,",",n+3);
}
window.alert(cArray[0];
window.alert(cArray[360];)// just to test
draw(cArray)} // this isnt relavent
};
The result should show an alert the first and last element in the cArray array but it doesnt show at all. What could be the issue?

Related

Looping through a table rows with comparisons using VUgen

I have a table I need to compare each of the values within to an existing parameter.
I have this Xpath here: //*[#id="maincontent"]/messages/div/div[1]/div[1]/table/tbody/tr[1]/td[4]/div/span
and would like to insert an increment variable from the loop to go through the /tr/ in the table only.
Here is what i have so far:
var i;
var max = 10;
var xpathleft = `*[#id="maincontent"]/messages/div/div[1]/div[1]/table/tbody/tr[`;
var xpathright = `]/td[4]/div/span`;
for (i = 1; i < max, i++)
{
var currentXpath = string.concat(xpathleft, i, xpathright);
}
if (currentXpath.innerHTML == PartnerIDs)
{
lr_log_message("Match Found!");
}
This is currently sitting in an Evaluate Javascript step in TruClient/VUgen and is giving me Syntax Error: Unexpected Token )
The element here doesn't have any ID I can reference and looks like this: Partner ID
and has been difficult to pull the needed Partner ID text within code.
Some of your JavaScript syntax is incorrect.
Try this:
var i;
var max = 10;
var xpathleft = `*[#id="maincontent"]/messages/div/div[1]/div[1]/table/tbody/tr[`;
var xpathright = `]/td[4]/div/span`;
for (i = 1; i < max; i++){
var currentXpath = `${xpathleft}${i}${xpathright}`;
if (currentXpath.innerHTML == PartnerIDs) {
lr_log_message("Match Found!");
}
}

Why does one function works, but the other does not?

This one works, it shows the table:
When I put .style.display = "tablerow", the function works
var getMain = document.getElementById("main").style.display = "table-row";
var getLocalStorage = localStorage.hasOwnProperty('tableFill');
for (i = 0; i < getLocalStorage.lenght; i++) {
if (getLocalStorage[i]){
getMain;
But when I put it in in the for loop it doesn't work anymore, but I also get no error at all in the console...
var getMain = document.getElementById("main");
var getLocalStorage = localStorage.hasOwnProperty('tableFill');
for (i = 0; i < getLocalStorage.lenght; i++) {
if (getLocalStorage[i]){
getMain.style.display = "table-row";
localStorage.hasOwnProperty('tableFill') returns true or false. Does not have any length so you won't get into the loop.
localStorage.hasOwnProperty('tableFill') returns a boolean to check if the property exists or not. It does not have data to make a loop out of it

Script executes properly in ExtendScript Toolkit, but not Illustrator

I've written a script that takes a selection of multiple paths, duplicates them, and applies "Object > Envelope distort > Make with top object" on each duplicate of the bottom path with every other path in the selection using an action (don't believe there's anything in the DOM for interacting with envelopes directly). So I start with this:
https://i.stack.imgur.com/cBKZs.png
It works perfectly in ExtendScript Toolkit, giving me this:
https://i.stack.imgur.com/dLdRz.png
But if I execute the script from within Illustrator, I get this mess:
https://i.stack.imgur.com/caL0u.png
Here's my code:
var doc = app.activeDocument;
var sel = app.activeDocument.selection;
var currentLayer = app.activeDocument.activeLayer;
function envelope(){
var arr = [];
var bottomObject = sel[sel.length - 1];
bottomObject.selected = false;
for (i = 0; i < sel.length - 1; i++){
arr.push(sel[i]);
var newObjs = sel[i].duplicate();
newObjs.zOrder(ZOrderMethod.SENDBACKWARD)
}
currentLayer.hasSelectedArtwork = false;
for (i = 0; i < arr.length; i++){
var objectsToDistribute = bottomObject.duplicate();
objectsToDistribute.zOrder(ZOrderMethod.SENDTOBACK);
arr[i].selected = true;
objectsToDistribute.selected = true;
app.doScript('Envelope all', 'scriptTest');
}
}
envelope();
Here's the action set. So why would I be getting different results from the same script? Is there a way to work around this from within Illustrator?
You have to move the line currentLayer.hasSelectedArtwork = false; into the loop. It's need to clear the selection for every iteration:
...
for (i = 0; i < arr.length; i++){
currentLayer.hasSelectedArtwork = false; // <------------- here
var objectsToDistribute = bottomObject.duplicate();
objectsToDistribute.zOrder(ZOrderMethod.SENDTOBACK);
arr[i].selected = true;
objectsToDistribute.selected = true;
app.doScript('Envelope all', 'scriptTest');
}
...
And actually you don't need the action set.
It can be done via app.executeMenuCommand('Make Envelope');
Here is my version of the script:
var sel = app.activeDocument.selection; // all the objects that have been selected
var lowest = sel[sel.length-1]; // the lowest object
for (var i=0; i<sel.length-1; i++) {
var top = sel[i].duplicate(); // make a copy of the next object
var btm = lowest.duplicate(); // make a copy of the lowest object
app.selection = null; // deselect all
top.selected = true; // select the top copy
btm.selected = true; // select the bottom copy
app.executeMenuCommand('Make Envelope'); // make Envenlope
}
The only difference: it transforms the lowest object among the selected objects.

Only the first element of the array has been output to the google sheet, I need all elements (except null/undefined elements) to be output

My script needs to get values from one sheet and put them into another sheet. It picks up the values correctly. However, when the values are being output to the other sheet only the first element is being output. I need all elements to be output. This could be achieved (I think) in one of two ways; either skip over/ignore the empty elements of the array when the values are being output or ignore the empty cells in the source sheet. Any help is appreciated. The script is below:
Updated code:
function exportArds() {
var ards = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Newtownards");
var export = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Export");
var ardsLastRow = ards.getLastRow();
var exportLastRow = export.getLastRow();
var refValues = new Array(6);
var amountValues = new Array(6);
//get values for export
refValues = ards.getRange("B12:G12").getValues();
amountValues = ards.getRange("B13:G13").getValues();
for(var i = 2; i<=6; i++){
var a = refValues.join().split(',').filter(Boolean);
var b = amountValues.join().split(',').filter(Boolean);
//get values, if any, in export sheet (used to check where the empty cells are for input)
var checkAmount = export.getRange(i, 4).getValue().toString();
var checkDescription = export.getRange(i, 5).getValue().toString();
export.getRange(i, 4).setValue(b[i-2]);
export.getRange(i, 5).setValue(a[i-2]);
}//close for loop
Logger.log(a);
Logger.log(b);
}//close function
When the values are being set to a cell correctly the next sheet however, if the array has say 2 values in it, the undefined will be output 4 times. Is there a way to prevent this?
I have found a solution that works for this problem, I'm sure it could be a bit cleaner so feel free to comment any suggestions! The code is below:
function exportArds() {
var ards = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Newtownards");
var export = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Export");
//get values for export
var refValues = ards.getRange("B12:G12").getValues();
var amountValues = ards.getRange("B13:G13").getValues();
for(var i = 2; i<=6; i++){
var a = refValues.join().split(',').filter(Boolean);
var b = amountValues.join().split(',').filter(Boolean);
}//close for loop
var aLength = a.length;
var bLength = b.length;
for(i = 2; i<=aLength; i++){
export.getRange(i, 5).setValue(a[i-2]);
}
for(i = 2; i<=bLength; i++){
export.getRange(i, 4).setValue(b[i-2]);
}
}//close function

Loop through array of images in javascript

I'm trying to loop through an array of images but can't seem to get past image 2.
The array should also loop back to 1 when the last image has passed...
var WorkArray = new Array('work/01.png', 'work/02.png', 'work/03.png', 'work/04.png');
var nelements = WorkArray.length;
preload_image_object = new Image();
var i = 0;
for(i=0; i<=nelements; i++) {
preload_image_object.src = WorkArray[i];
}
function cC() {
var nelements = WorkArray.length;
var i = 0;
for(i=0; i<=nelements; i++) {
nelements = WorkArray[i];
}
document.getElementById("work").style.backgroundImage="url('"+WorkArray[i]+"')";
}
You can save the current file and use modulo to run in cyclic manner.
It will look something like that:
var WorkArray = new Array('work/01.png', 'work/02.png', 'work/03.png', 'work/04.png');
var currentImage = 0
function nextImage(){
currentImage = (currentImage + 1) % WorkArray.length;
document.getElementById("work").style.backgroundImage="url('"+WorkArray[currentImage]+"')";
}
You are overwriting nelements with the current element of the loop:
nelements = WorkArray[i];
The following should fix your loops:
var WorkArray = new Array('work/01.png', 'work/02.png', 'work/03.png', 'work/04.png');
var preload_image_object = new Image();
/* Lets get rid of `nelements`, as its just confusing. Get the length here.
* If, for performace reasons you want to use elements, the best way is to reverse
* aka for(var i = WorkArray.length-1; i >= 0 ; i--)
* Also, its simpler to declare the var in your for-loop itself instead of outside of it.
*/
for(var i = 0; i <= WorkArray.length; i++){
preload_image_object.src = WorkArray[i];
}
Also, again for simplifications sake, your application of the background-image could be done inside your for loop as well, and can be made to look cleaner with some spaces and omitting the ' inside your url():
document.getElementById("work").style.backgroundImage = "url(" + WorkArray[i] + ")";

Categories

Resources