2

I'm trying to replace blank cells in column B (column name= Status) with the text "null". I have two different approaches that get me halfway there, however I can't figure out how to fully solution this. Any help would be greatly appreciated

Here's the excel table showing column B and a few cells in that column with blank cells

excel screenshot of table

Here's two different scripts I created trying to solution this

Script 1= I can identify the blank cells, but cannot figure out how to set the values for the cells.

function main(workbook: ExcelScript.Workbook) {
let source = workbook.getWorksheet("Source");
let sourceTable = workbook.getTable("Source");
const statusVisibleRange = source.getUsedRange().getColumn(0);
const statusVisibleRangeShifted = statusVisibleRange.getOffsetRange(1,1);
const StatusFilter = "Status";
const StatusFilterValues = '';
let stringValue = "null";

let blankCells = 
statusVisibleRangeShifted.getUsedRange().getSpecialCells(ExcelScript.SpecialCellType.blanks);
console.log(blankCells.getAddress());
}

Script 2=Script works if there are blank cells in column B (column name = Status), however if there are not any blank cells...it's filtering to all values and updating the statuses of everything in this column to "null".

function main(workbook: ExcelScript.Workbook) {
let source = workbook.getWorksheet("Source");
let sourceTable = workbook.getTable("Source");
const statusVisibleRange = source.getUsedRange().getColumn(0);
const statusVisibleRangeShifted = statusVisibleRange.getOffsetRange(1,1);
const StatusFilter = "Status";
const StatusFilterValues = '';
let stringValue = "null";

const statusFilter = sourceTable.getColumnByName(StatusFilter);
statusFilter.getFilter().applyValuesFilter([StatusFilterValues]);

statusVisibleRangeShifted.getUsedRange().setValue(stringValue);

statusFilter.getFilter().clear();
Skin
  • 9,085
  • 2
  • 13
  • 29
ckatz
  • 23
  • 8

1 Answers1

1

This worked for me ...

function main(workbook: ExcelScript.Workbook)
{
  let worksheet = workbook.getWorksheet("Source");
  let table = worksheet.getTable("Source");

  let statusColumn = table.getColumnByName("Status");
  let statusColumnRange = statusColumn.getRangeBetweenHeaderAndTotal();

  let emptyStatusCells = statusColumnRange.getSpecialCells(ExcelScript.SpecialCellType.blanks);

  if (emptyStatusCells != undefined) {
    let rangeAreas = emptyStatusCells.getAreas();

    rangeAreas.forEach(range => {
      let values = range.getValues();

      values.forEach(cellValue => {
        cellValue[0] = "null";
      })

      range.setValues(values);
    })
  }

  // Add additional logic here once the blank cells are dealt with.
}
Skin
  • 9,085
  • 2
  • 13
  • 29
  • One issue after doing some testing with this script. If there are not any blanks in the column, the script fails. It gives me this error. Line 11: Cannot read properties of undefined (reading 'getAreas') – ckatz Apr 13 '22 at 12:25
  • A quick IF statement can fix that. Answer updated. – Skin Apr 13 '22 at 12:28
  • Unfortunately it resolved the error, but is stopping the script if there aren't any blank cells. I want the script to skip the immediate next step and continue. – ckatz Apr 13 '22 at 21:40
  • You can’t keep accepting the answer and then changing it. Your question was how to update the cells, I did that. Now you’re wanting free consulting. Error checking and other scenarios outside the question are your responsibility. – Skin Apr 13 '22 at 21:41
  • Thanks for all your help, I'm very grateful. Sorry - I'm not familiar with how this platform works so I can get other input if your solution doesn't cover all of my original needs. As I posted in my original request for help, I can't get the solutions to work if there aren't any blank cells in the column. – ckatz Apr 13 '22 at 21:49
  • That’s ok, happy to help but once the question is answered, it’s etiquette leave the answer checked unless there’s a fundamental issue that relates directly to your question. If I understand you correctly, you want to know where to put additional logic after the fact, you just need to do that outside the If statement. Have updated the answer to include a comment for where you need to do that. – Skin Apr 13 '22 at 22:03
  • 1
    Thanks for clarifying. It's working now, I was missing one more } .Thank you for all your help! – ckatz Apr 13 '22 at 22:20
  • I got this error always, can you help me with any solution for that, `Error Line 158: RangeAreas getAreas: The response payload size has exceeded the limit. Please refer to the documentation: "https://learn.microsoft.com/office/dev/add-ins/concepts/resource-limits-and-performance-optimization#excel-add-ins".` – AlameerAshraf May 18 '22 at 15:01
  • Can you please open a new question? Include your complete script and any screenshots of data so it’s clear on what the issue is.. – Skin May 18 '22 at 19:48