Skip to main content
dublove
Legend
July 10, 2024
Question

This Script is use to convert first row to header, the last row to footer, it delete my content.

  • July 10, 2024
  • 1 reply
  • 949 views

This Script is use to convert first row to header, the last row to footer, it delete my content.
Sorry, The original post seemed to be posted by me. But for too long, I didn't find it.
The initial purpose: 

It will judge the last row of the table.

If it is empty, it is directly converted to the footer;if it is not empyt(have content), add 1 row Below the last row, and convert the new row to footer.

It also convert first row to header, apply the  cellstyle.
But now it delete the last row, that it is Non -empty line. my content lost.

 

 

 

 

table = app.selection[0];
app.selection[0].appliedTableStyle = "mytab";
var myHeaderRow = table.rows[0];
var myNextRow = table.rows[-2];
var myFooterRow = table.rows[-1];

for (var i = 0; i < table.rows.length - 1; i++) {
  table.rows[i].cells.everyItem().minimumHeight = "7.6mm";
};


// Optional:
myFooterRow.cells.everyItem().contents = "";
// Not optional. 
// Set the point size in a value, so that the text could be visible despite the minimal height of the cells:
myFooterRow.cells.everyItem().texts.everyItem().pointSize = 0.1;
myFooterRow.cells.everyItem().appliedCellStyle = "footer";
myHeaderRow.cells.everyItem().appliedCellStyle = "header";
myFooterRow.cells.everyItem().topInset = 0;
myFooterRow.cells.everyItem().leftInset = 0;
myFooterRow.cells.everyItem().bottomInset = 0;
myFooterRow.cells.everyItem().rightInset = 0;

myFooterRow.cells.everyItem().height = "1.06mm";//行高
//myNextRow.cells.everyItem().minimumHeight  = "7.5mm";


if (!(table.rows[0].rowType == RowTypes.HEADER_ROW)) {
  table.rows[0].rowType = RowTypes.HEADER_ROW;
}
else {
  //alert("header existed");
};
if (!(myFooterRow.rows.item(-1).rowType == RowTypes.FOOTER_ROW)) {
  myFooterRow.rows.item(-1).rowType = RowTypes.FOOTER_ROW;
}
else {
  //alert("end of the table exists");
};

 

 

 

 

1 reply

Robert at ID-Tasker
Legend
July 10, 2024

What kind of translating software are you using? 

 

Can you post your question in your native language? 

 

dublove
dubloveAuthor
Legend
July 11, 2024

It should be easy to understand now.

Community Expert
July 11, 2024

I'd refer to @Robert at ID-Tasker  as more experienced and better at this than I am

However, I attempted to correct your script, I'm not a professional scripter/coder - I am just learning

 

Looking forward to how this works - as it seems useful to me too

 

var table = app.selection[0];
table.appliedTableStyle = "mytab";
var myHeaderRow = table.rows[0];
var myFooterRow = table.rows[-1];

// Convert first row to header and apply cell style
myHeaderRow.cells.everyItem().appliedCellStyle = "header";
if (myHeaderRow.rowType !== RowTypes.HEADER_ROW) {
    myHeaderRow.rowType = RowTypes.HEADER_ROW;
}

// Check if the last row is empty
var isLastRowEmpty = true;
for (var j = 0; j < myFooterRow.cells.length; j++) {
    if (myFooterRow.cells[j].contents !== "") {
        isLastRowEmpty = false;
        break;
    }
}

// If the last row is not empty, add a new row for the footer
if (!isLastRowEmpty) {
    table.rows.add();
    myFooterRow = table.rows[-1];
}

// Convert the last row to footer and apply cell style
myFooterRow.cells.everyItem().contents = "";
myFooterRow.cells.everyItem().texts.everyItem().pointSize = 0.1;
myFooterRow.cells.everyItem().appliedCellStyle = "footer";
myFooterRow.cells.everyItem().topInset = 0;
myFooterRow.cells.everyItem().leftInset = 0;
myFooterRow.cells.everyItem().bottomInset = 0;
myFooterRow.cells.everyItem().rightInset = 0;
myFooterRow.cells.everyItem().height = "1.06mm";

if (myFooterRow.rowType !== RowTypes.FOOTER_ROW) {
    myFooterRow.rowType = RowTypes.FOOTER_ROW;
}

// Set the minimum height for all rows except the last one
for (var i = 0; i < table.rows.length - 1; i++) {
    table.rows[i].cells.everyItem().minimumHeight = "7.6mm";
}