Forum Discussion
Remove strikethrough formatted characters in a cell
Hi,
I am trying to remove the all characters in a selected cell that is formatted with strikethrough. I managed the Office script below but it's only removing the strikethrough format and doesn't delete the 'strikethrough formatted' word. Any suggestions how I can modify the script to achieve my objective?
function main(workbook: ExcelScript.Workbook) {
// Get the active cell
let cell = workbook.getActiveCell();
// Get the text from the cell
let text = cell.getText();
let sheet = workbook.getActiveWorksheet();
// Get the selected range
// let range = sheet.getSelectedRange();
// Get the format of each character in the cell
// Get the format of each character in the cell
let formats = cell.getFormat().getFont().getStrikethrough();
// Initialize a new string to store the result
let newText = '';
// Loop through each character in the cell text
for (let i = 0; i < text.length; i++) {
// Check if the character is formatted with strikethrough
if (!formats) {
// If not, add it to the new text
newText += text[i];
}
}
// Set the new text to the cell
cell.setValue(newText);
}
getFont() is applied to entire cell, and .getFont().getStrikethrough() returns true if entire cell is strikethrough formatted, false if entire cell has no such format and null if it is partially formatted.
OfficeScript doesn't work with partial formatting, that is confirmed here typescript - Office Scripts - Excel: format part of strings - Stack Overflow
3 Replies
- SergeiBaklanDiamond Contributor
getFont() is applied to entire cell, and .getFont().getStrikethrough() returns true if entire cell is strikethrough formatted, false if entire cell has no such format and null if it is partially formatted.
OfficeScript doesn't work with partial formatting, that is confirmed here typescript - Office Scripts - Excel: format part of strings - Stack Overflow
- afhamiltonCopper ContributorThanks Sergei! That sucks, hopefully Microsoft starts to support this sometime in the near future.
- SergeiBaklanDiamond Contributor
afhamilton , you are welcome. Yes, hope it'll be done.