Editing a Numbers script to enter form data in the first empty line of a table with a last header row

I would need some help to edit a script.

The current one allows me to fill in the table starting from a form and it does it by adding a line, but it doesn't work if the last header line.

I would need you to enter the form data in the first empty line of the table even with the last header line. A big thank you to those who can help me.

This is the script text I'm using now.



// get the two tables involved

var Numbers = Application("Numbers");

var database = Numbers.documents[0].sheets[0].tables["Anagrafe 2025"];

var form = Numbers.documents[0].sheets[1].tables["FORM"];

// get all the values from the form

var values = [];

for(var i=0;i<form.rows.length;i++) {

var value = form.rows[i].cells[0].value();

values.push(value);

}

// create a new row at the bottom of the database

var newRow = database.cells["A"+database.rows.length].addRowBelow();

// put the values into that row

for(var i=0;i<values.length;i++) {

newRow.cells[i].value = values[i];

}

// clear out the form, going backwards so to end on the first cell

for(var i=form.rows.length-1;i>=0;i--) {

form.rows[i].cells[0].value = "";

}


[Re-Titled by Moderator]

iMac 21.5″, macOS 10.15

Posted on May 4, 2025 12:02 PM

Reply
Question marked as Top-ranking reply

Posted on May 4, 2025 8:37 PM

Dubbia wrote:

enter the form data in the first empty line of the table even with the last header line.


I'm guess by "last header line" that you mean you have a Footer Row at the bottom of the table and you want to add a row above that and transfer the values from the table "Form". If so, then you can subtract footerRowCount before addRowBelow, something like this:


// get the two tables involved
var Numbers = Application("Numbers");
var database = Numbers.documents[0].sheets[0].tables["Anagrafe 2025"];
var form = Numbers.documents[0].sheets[1].tables["FORM"];
// get all the values from the form
var values = [];
for(var i=0;i<form.rows.length;i++) {
var value = form.rows[i].cells[0].value();
values.push(value);
}
// create a new row at the bottom of the database
var newRow = database.cells["A"+(database.rows.length-database.footerRowCount())].addRowBelow();
// put the values into that row
for(var i=0;i<values.length;i++) {
newRow.cells[i].value = values[i];
}
// clear out the form, going backwards so to end on the first cell
for(var i=form.rows.length-1;i>=0;i--) {
form.rows[i].cells[0].value = "";
}


SG


P.S. Good to see an example of JXA scripting of Numbers in these forums.

2 replies
Question marked as Top-ranking reply

May 4, 2025 8:37 PM in response to Dubbia

Dubbia wrote:

enter the form data in the first empty line of the table even with the last header line.


I'm guess by "last header line" that you mean you have a Footer Row at the bottom of the table and you want to add a row above that and transfer the values from the table "Form". If so, then you can subtract footerRowCount before addRowBelow, something like this:


// get the two tables involved
var Numbers = Application("Numbers");
var database = Numbers.documents[0].sheets[0].tables["Anagrafe 2025"];
var form = Numbers.documents[0].sheets[1].tables["FORM"];
// get all the values from the form
var values = [];
for(var i=0;i<form.rows.length;i++) {
var value = form.rows[i].cells[0].value();
values.push(value);
}
// create a new row at the bottom of the database
var newRow = database.cells["A"+(database.rows.length-database.footerRowCount())].addRowBelow();
// put the values into that row
for(var i=0;i<values.length;i++) {
newRow.cells[i].value = values[i];
}
// clear out the form, going backwards so to end on the first cell
for(var i=form.rows.length-1;i>=0;i--) {
form.rows[i].cells[0].value = "";
}


SG


P.S. Good to see an example of JXA scripting of Numbers in these forums.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Editing a Numbers script to enter form data in the first empty line of a table with a last header row

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.