WxShady13
Board Regular
- Joined
- Jul 24, 2018
- Messages
- 185
- Office Version
- 365
- Platform
- Windows
- Mobile
When I record a script the first step is to create a table from the data. Obviously the data could be any amount of rows, however it is a set number of columns. When I attempt to run the script (even on the same data without the table) I get an error. Here is the Code for the script. The error is Line 4 (which begins with let newTable) Workbook addTable: You cannot perform the requested operation. Why will it not at least create the table with the provided range, and how would I make it create the table dynamically to account for any amount of rows?
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Add a new table at range A1:R87 on selectedSheet
let newTable = workbook.addTable(selectedSheet.getRange("A1:R87"), true);
// Set wrap text to false for table newTable
newTable.getRange().getFormat().setWrapText(false);
// Indent set to 0 for table newTable
newTable.getRange().getFormat().setIndentLevel(0);
// Insert at range M:M on selectedSheet, move existing cells right
selectedSheet.getRange("M:M").insert(ExcelScript.InsertShiftDirection.right);
// Set number format for range M:M on selectedSheet
selectedSheet.getRange("M:M").setNumberFormatLocal("0.00");
// Set range M2 on selectedSheet
selectedSheet.getRange("M2").setFormulaLocal("=DAYS(TODAY(),L2)");
let conditionalFormatting: ExcelScript.ConditionalFormat;
// Create cell value criteria for range M:M on selectedSheet
conditionalFormatting = selectedSheet.getRange("M:M").addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue);
conditionalFormatting.getCellValue().setRule({operator: ExcelScript.ConditionalCellValueOperator.greaterThan, formula1: "=29"});
conditionalFormatting.getCellValue().getFormat().getFill().setColor("#ffc7ce");
conditionalFormatting.getCellValue().getFormat().getFont().setColor("#9c0006");
conditionalFormatting.setStopIfTrue(false);
conditionalFormatting.setPriority(0);
// Sort on table: newTable column index: '12'
newTable.getSort().apply([{key: 12, ascending: true}]);
}
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Add a new table at range A1:R87 on selectedSheet
let newTable = workbook.addTable(selectedSheet.getRange("A1:R87"), true);
// Set wrap text to false for table newTable
newTable.getRange().getFormat().setWrapText(false);
// Indent set to 0 for table newTable
newTable.getRange().getFormat().setIndentLevel(0);
// Insert at range M:M on selectedSheet, move existing cells right
selectedSheet.getRange("M:M").insert(ExcelScript.InsertShiftDirection.right);
// Set number format for range M:M on selectedSheet
selectedSheet.getRange("M:M").setNumberFormatLocal("0.00");
// Set range M2 on selectedSheet
selectedSheet.getRange("M2").setFormulaLocal("=DAYS(TODAY(),L2)");
let conditionalFormatting: ExcelScript.ConditionalFormat;
// Create cell value criteria for range M:M on selectedSheet
conditionalFormatting = selectedSheet.getRange("M:M").addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue);
conditionalFormatting.getCellValue().setRule({operator: ExcelScript.ConditionalCellValueOperator.greaterThan, formula1: "=29"});
conditionalFormatting.getCellValue().getFormat().getFill().setColor("#ffc7ce");
conditionalFormatting.getCellValue().getFormat().getFont().setColor("#9c0006");
conditionalFormatting.setStopIfTrue(false);
conditionalFormatting.setPriority(0);
// Sort on table: newTable column index: '12'
newTable.getSort().apply([{key: 12, ascending: true}]);
}