I’m working with ExcelScript and encountering an issue with a loop that copies a range of cells. The loop is intended to copy the range adjustedN times, where adjustedN is calculated as Math.floor((numberOfPages – 16) / 2). However, the loop always copies the range 4 times regardless of the calculated value of adjustedN. I don’t know how to fix it, because the calculation is correct.
I’d like that range be copied AdjustedN times as the looping and don’t 4 times always, dont matter N value.
// Calculate the adjusted number of iterations ((N-16)/2)
const adjustedN = Math.floor((numberOfPages - 16) / 2);
const startColumn = 2; // Column B
const startRow = 41;
const endColumn = 59; // Column BH
let currentColumn = startColumn;
let currentRow = startRow;
let pageCounter = 0;
// Get the destination range
const destinationRange = copiedSheet.getRangeByIndexes(currentRow - 1, currentColumn - 1, 14, 52); // Assuming the template range is 14 rows by 52 columns
// Use a for loop to copy the range adjustedN times
for (let i = 0; i < adjustedN; i++) {
// Copy values and formatting from the template range
destinationRange.copyFrom(templateSheet.getRange(rangeToCopyForN16), ExcelScript.RangeCopyType.all);
// Increment page numbers for the copied range
incrementPagNumbers(copiedSheet, destinationRange.getAddress(), 15 + pageCounter, 1);
pageCounter++;
}
Yeeg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.