This content has been marked as final.
Show 9 replies
-
1. Re: JS Working with an array inside of an array
Harbs. Aug 20, 2008 11:46 PM (in response to John.Kordas)How 'bout something like this?<br /><br />function DecomposeNumberRange(numberRange){<br /> var retVal=[];<br /> for(var i=0;i<numberRange.length;i++){<br /> numberRange[i]=numberRange[i].split("-");<br /> if(numberRange[i].length==1){<br /> retVal.push(Number(numberRange[i][0]));<br /> }<br /> else{<br /> var endNumber=Number(numberRange[i][1]);<br /> var startNumber = Number(numberRange[i][0]);<br /> while(startNumber<=endNumber){<br /> retVal.push(startNumber);<br /> startNumber++;<br /> }<br /> }<br /> }<br /> return retVal;<br /> }<br /><br /><br />Harbs -
2. Re: JS Working with an array inside of an array
pkahrel Aug 21, 2008 12:24 AM (in response to John.Kordas)John,
Why do you want to unrange any page ranges? You can enter them in the range field in the print dialog in the UI, and in a script you can say this:
myDoc.printPreferences.pageRange = "1,5,6-9,11";
Peter -
3. Re: JS Working with an array inside of an array
John.Kordas Aug 21, 2008 6:59 PM (in response to John.Kordas)Thanks guys I'm a little closer.
Peter I'm using an example Olav put together:
Olav Kvern, "Need script for printing Postscript files (ID CS2)" #4, 19 Nov 2007 4:45 pm
the reason is that his example allows you to output a document as single pages. To do this manually you need to send each page on its own. The script will do it in one hit. I've made some changes that allow the used to specify a single page if they need but would like to be able to send 1,3,5-6,9,12 for example.
If I use myDoc.printPreferences.pageRange = "1,5,6-9,11"; it will create one pdf with the pages 1,5,6-9,11 inside it.
Harbs when I tried your script it returned this in the console 1,NaN,5,NaN,6,NaN,9,NaN,1,1. It did give me an idea and I've come up with this.
var numbers, mynumbers, mydnumbers ;
numbers = "1,5,6-9,11";
var numbersarray = new Array;
numbersarray = numbers.split(',');
var mynumbers = numbersarray;
var countarray = numbersarray.length;
for(var myCounter = 0; myCounter < numbersarray.length; myCounter++){
//alert("length is "+mynumbers[myCounter].length);
if(/-/.test(mynumbers[myCounter]) == false){
alert(mynumbers[myCounter]);
}
if (mynumbers[myCounter].length >2 && /-/.test(mynumbers[myCounter])){
alert("dash num are "+mynumbers[myCounter]);
var dasharray = new Array;
dasharray = mynumbers[myCounter].split('-');
var dashnumbers = dasharray;
//alert("dash "+dashnumbers);
for (i=dashnumbers[0]; i < dashnumbers[1]; i++){
var count = dashnumbers[0];
alert(count);
}
count++;
}
}
For some reason count sticks at 6 and count++ does not loop to 7, 8, then 9. Any suggestions? -
4. Re: JS Working with an array inside of an array
John.Kordas Aug 21, 2008 7:46 PM (in response to John.Kordas)OK this fixed the count++
var dasharray = new Array;
dasharray = mynumbers[myCounter].split('-');
var dashnumbers = dasharray;
var start = parseInt(dashnumbers[0]);
var end = (parseInt(dashnumbers[1])+1);
alert("dash "+end);
for (i=start; i < end; i++){
var count = i;
alert(count);
So I now get alerts of 1,5,6,7,8,9,11.
Just need to put it in my script and hope it works.
Cheers. -
5. Re: JS Working with an array inside of an array
pkahrel Aug 22, 2008 12:05 AM (in response to John.Kordas)John,
I thought you were sending pages to a printer, hadn't realised that it had to go to PDF as separate pages. Your script displays the following alerts in succession:
1
5
dash num are 6-9
dash 10
6
7
8
9
11
I use a script to combine index references, which (among other things) needs to unravel page ranges. This is the (slightly modified) function I use:
unranged_array = unrange ("1,5,6-11")
function unrange (s)
{
return s.replace (/(\d+)-(\d+)/g, expand)
}
function expand ()
{
var expanded = [];
var start = Number (arguments[1]);
var stop = Number (arguments[2]);
for (var i = start; i <= stop; i++)
expanded.push (i);
return expanded
}
Peter -
6. Re: JS Working with an array inside of an array
Harbs. Aug 22, 2008 12:26 AM (in response to pkahrel)Peter,
Very nice code!
John, my function was meant to accept an array of pages or page ranges.
Not a string...
(It returns an expanded array as well...)
Harbs -
7. Re: JS Working with an array inside of an array
pkahrel Aug 22, 2008 1:00 AM (in response to John.Kordas)Thanks, Harbs. This kind of stuff always seems like black magic to me, but when I close my eyes and type away, it sometimes works :)
Peter -
8. Re: JS Working with an array inside of an array
John.Kordas Aug 24, 2008 3:45 PM (in response to John.Kordas)That's brilliant Peter,
I knew there had to be a better way of doing it.
Much appreciated. -
9. Re: JS Working with an array inside of an array
(Dave_Saunders) Oct 30, 2008 4:59 AM (in response to John.Kordas)Peter,
This is the first time I've looked closely at what you posted here. That is a very instructive example.
Thank you very much.
Dave



