I am trying to write the converted values of a selected range to the adjacent column of cells. I am able to get the values written to the cells, however, it only prints out the value of the last item in the range to all of the cells. I've included a screenshot of what the output is, along with my code.
I have read through the documentation and tried several ways to do this, but keep getting the same result and so I am stumped. Any assistance would be greatly appreciated!
Here is the current output:
function main(workbook: ExcelScript.Workbook) {
// Get the currently highlighted range in the workbook.
let range = workbook.getSelectedRange();
let rangeAddress = range.getAddress();
// Get the that range's value
let cellInputs = range.getValues();
let timeInSecondsArr: Array<number>;
timeInSecondsArr = [];
// Get cell values and add to array
cellInputs.forEach((columnItem, columnIndex) => {
cellInputs[columnIndex].forEach((rowItem, rowIndex) => {
timeInSecondsArr.push(Number(rowItem));
});
});
// Initialize array to add converted values to.
let convertedValuesArr: Array<string>;
convertedValuesArr = [];
function convertValues() {
let result = "";
// convert the values.
for (let i = 0; i < timeInSecondsArr.length; i++) {
let timeInSeconds = timeInSecondsArr[i];
let rightCell = range.getOffsetRange(0, 1);
// Get the value for the ss in h:mm:ss
function getSeconds() {
let seconds = Math.floor(timeInSeconds % 60);
return seconds;
}
// Get the value for the mm in h:mm:ss
function getMinutes() {
let m = Math.floor(timeInSeconds / 60);
let minutes = m % 60;
return minutes;
}
// Get the value for h in h:mm:ss
function getHours() {
let h = Math.floor(timeInSeconds / 60);
let hours = Math.floor(h / 60);
return hours;
}
result = (`${getHours()}:${getMinutes()}:${getSeconds()}`);
convertedValuesArr.push(result);
}
return result;
}
convertValues();
let rightCells = range.getOffsetRange(0, 1);
for(let i = 0; i < convertedValuesArr.length; i++){
rightCells.setValue(convertedValuesArr[i]);
}
}