I have a table that a user fills a Base column with values and the following columns might have empty values that I fill with that Base column values:
I did the code to do that logic, loading the columns and rows, modifying them separately and then I want to load them into the sheet just to see if it's correct:
Excel.run(function (context) {
var currentWorksheet = context.workbook.worksheets.getActiveWorksheet();
var inputsTable = currentWorksheet.tables.getItem("Inputs");
var baseValues = inputsTable.columns.getItemAt(1).load("values");
var columnsLoad = inputsTable.columns.load("values");
var rowsLoad = inputsTable.rows.load("values");
return context.sync()
.then(function () {
var rows = rowsLoad.items;
var columns = columnsLoad.items;
// Populate base
for (let i = 2; i < columns.length; i++) {
for (let j = 1; j < rows.length; j++) {
if(columns[i][j] == "")
columns[i][j] = baseValues.values[j];
}
}
// Write data from table back to the sheet
currentWorksheet.getRange("A25:A48").values = baseValues.values;
currentWorksheet.getRange("B25:B48").values = columns[2].values;
// Sync to update the sheet in Excel
return context.sync();
});
})
I'm failing to do it because the writing test is showing me the data not modified:
When the expected should be this:
What am I doing wrong?
After doing the smart decision to debug this in a Web browser and understanding better how the objects work, I've come up with this solution:
Excel.run(function (context) {
var currentWorksheet = context.workbook.worksheets.getActiveWorksheet();
var inputsTable = currentWorksheet.tables.getItem("Inputs");
var baseValues = inputsTable.columns.getItemAt(1).load("values");
var columnsLoad = inputsTable.columns.load("values");
var rowsLoad = inputsTable.rows.load("values");
return context.sync()
.then(function () {
var rows = rowsLoad.items;
var columns = columnsLoad.items.slice();
// Populate base
for (let i = 2; i < columns.length; i++) {
let column = columns[i].values
for (let j = 1; j < rows.length; j++) {
if(column[j][0] == ""){
column[j][0] = baseValues.values[j][0];
}
}
}
console.log(columns[2].values)
// Write data from table back to the sheet
currentWorksheet.getRange("A25:A46").values = baseValues.values;
currentWorksheet.getRange("B25:B46").values = columns[2].values;
// Sync to update the sheet in Excel
return context.sync();
});
})