I need help creating an Excel office script that can automate some formatting within each sheet of the workbook.
This is what I have done so far and it just seems to insert a blank column anyway. Not sure where I am going wrong. It is adding a column no matter if Column A is blank or not. It is also not reading all of the sheets within the workbook.
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Set range Z1 on selectedSheet
selectedSheet.getRange("Z1").setFormula("=IF(A1<>\"\",1,0)");
// Get the value in cell A1
let value = selectedSheet.getRange("A1").getValue();
// Check if the value in A1 is 0
if (value = 0) {
// Insert Column
selectedSheet.getRange("A:A").insert(ExcelScript.InsertShiftDirection.right);
} else {
// If value 1, leave alone.
console.log("All good.");
}
}
Do you mean that you want to check the value of cell A1 in each sheet and inset a column if it's not blank?
function main(workbook: ExcelScript.Workbook) {
let sheets = workbook.getWorksheets();
sheets.forEach(sheet => {
let cellValue = sheet.getRange("A1").getValue();
if (cellValue === "") {
sheet.getRange("A1").getEntireColumn().insert(ExcelScript.InsertShiftDirection.right);
}
});
}