Copy link to clipboard
Copied
I am relatively new to working with scripts in InDesign and have been reading "InDesign CS5 Automation Using XML and Javascript" by Grant Gamble. There's a script I'm trying to execute to update a table in a publication, and I keep getting a Javascript error (Error number: 21 Error String: undefined is not an object) when it executes a function. The error dialog points to the line of code I've bolded below:
function updateTables(){
var doc = app.activeDocument;
for (var i =0; i < doc.pages.length; i++){
var currentPage = doc.pages[i];
var currentFrame;
if(currentPage.textFrames.length > 0 && currentPage.textFrames[0].tables.length > 0){
}
else{
continue;
}
var tableCount = currentFrame.tables.length;
if(tableCount > 0){
var currentTable = currentFrame.tables[0];
var rowCount = currentTable.rows.length;
var currentCuisine = currentFrame.paragraphs[0].contents.replace("\n", "");
for (var j = 0; j < rowCount; j++){
var currentRow = currentTable.rows[j];
var currentCourse = currentRow.cells[0].contents;
var currentKey = currentCuisine + currentCourse;
if(currentKey in g.objDates){
currentRow.contents = g.objDates[currentKey];
}
}
}
}
//
I appreciate any guidance on debugging it. Thank you!
2 Correct answers
You haven't set the variable currentFrame to anything. var currentPage declares a variable as undefined until you assign a value, eg.
currentFrame = currentPage.textFrames[0];
if (currentFrame.isValid) {
// do something here
}
You can check the validity of currentPage and currentTable also.
- Mark
Hi @sanjayb75202732 ,
Error showing because you did not assign value to the variable.
updateTables();
function updateTables() {
var doc = app.activeDocument;
for (var i = 0; i < doc.pages.length; i++) {
var currentPage = doc.pages[i];
var currentFrame;
if (currentPage.textFrames.length > 0 && (currentFrame = currentPage.textFrames[0]).tables.length > 0) {
var tableCount = currentFrame.tables.length;
if (tableCount > 0) {
var
...
Copy link to clipboard
Copied
You haven't set the variable currentFrame to anything. var currentPage declares a variable as undefined until you assign a value, eg.
currentFrame = currentPage.textFrames[0];
if (currentFrame.isValid) {
// do something here
}
You can check the validity of currentPage and currentTable also.
- Mark
Copy link to clipboard
Copied
To add on to what Mark said, I would say it's good practice to check isValid if there's a chance that object might not exist. For instance if currentFrame doesn't have a table, then currentTable.length would also error. Edit: nevermind I see you are checking currentFrame.tables.length, which also works for the most part.
Copy link to clipboard
Copied
Hi @sanjayb75202732 ,
Error showing because you did not assign value to the variable.
updateTables();
function updateTables() {
var doc = app.activeDocument;
for (var i = 0; i < doc.pages.length; i++) {
var currentPage = doc.pages[i];
var currentFrame;
if (currentPage.textFrames.length > 0 && (currentFrame = currentPage.textFrames[0]).tables.length > 0) {
var tableCount = currentFrame.tables.length;
if (tableCount > 0) {
var currentTable = currentFrame.tables[0];
var rowCount = currentTable.rows.length;
var currentCuisine = currentFrame.paragraphs[0].contents.replace("\n", "");
for (var j = 0; j < rowCount; j++) {
var currentRow = currentTable.rows[j];
var currentCourse = currentRow.cells[0].contents;
var currentKey = currentCuisine + currentCourse;
if (currentKey in g.objDates) {
currentRow.contents = g.objDates[currentKey];
}
}
}
}
}
}
Copy link to clipboard
Copied
Thank you for helping me identify the bug!

