Copy link to clipboard
Copied
Hi guys,
Why does ID not allow you to delete all sections.
for ( i = allSections.length; i > 0; i-- ) {
allSections.remove();
}
This says undefined is not an object but I have 2 sections in my document.
allSections = doc.sections.everyItem().getElements();
A document must have at least one section with at least one page.
P.
Copy link to clipboard
Copied
A document must have at least one section with at least one page.
P.
Copy link to clipboard
Copied
Hi,
Are these 3 codes equivalent?
Thanks in advance!
app.activeDocument.sections.itemByRange(1, app.activeDocument.sections.length-1).remove();
allSections = app.activeDocument.sections.everyItem().getElements();
S = allSections.length;
for ( var s = 1 ; s < S ; s++) allSections
.remove();allSections = app.activeDocument.sections.everyItem().getElements();
for ( s = allSections.length-1 ; s > 0 ; s-- ) allSections
.remove();
(^/)
Copy link to clipboard
Copied
Well, Michel, they have the same result so in that sense they're equivalent. The approaches are slightly different. So it depends on what you understand by 'equivalent'.
P.
Copy link to clipboard
Copied
Peter,
Can I use them indifferently? … or could one of them be risky to be used in certain situations?
(^/)
Copy link to clipboard
Copied
I think they're equally safe. There may be performance differences, I don't know. Probably depends on the size of the document, the number of sections, the number of pages per section, etc. You could do a comparison of the three methods, on a smallish document and on a big one and compare the results.
P.
Copy link to clipboard
Copied
app.activeDocument.sections.itemByRange(1, app.activeDocument.sections.length-1).remove();
will throw an error if there's only one section.
ItemByRange is generally (considerably) quicker
Copy link to clipboard
Copied
Thank you all for your help and useful comments!
I made this function which does what I needed.
Copy link to clipboard
Copied
While Peter is right (naturally), it's not the reason why you get that error.
If you have 2 sections, the count is '2' but since counting starts at 0, their indexes are 0 and 1.
You try to remove a section with index 2, which does not exist. So you get that error.
Copy link to clipboard
Copied
> You try to remove a section with index 2, which does not exist. So you get that error.
I read the first sentence and thought that I had seen enough. Clearly not!