如何使用 Javascript Excel API 将 XML <br /> 替换为换行符



我有一个电子表格,其中包含多个包含要删除的XML格式的单元格。问题是我不能只删除换行符,因为它会破坏文本的格式。如何将<br />替换为换行符?

function run() {
return Excel.run(function(context) {
const sheet1 = context.workbook.worksheets.getItem("Sheet1");
const sheet2 = context.workbook.worksheets.getItem("Sheet2");
const sourceCell = sheet1.getCell(1,1).load("text") 
const destinationCell = sheet2.getCell(1,1)
return context.sync().then(function() {
destinationCell.values = sourceCell.text
})
})
}

假设我的源单元格包含以下文本:

[["08:00 - 08:30 brushing teeth<br />08:30 - 09:00 eating breakfast<br />09:00 - 09:30 ride the bus"]]

我将如何在目标单元格中用换行符替换<br />

range.values接受 2D 数组,因此您可以解析字符串并通过<br />将其剪切,将其分配给字符串数组

let mytexts : string[][];
// logic to cut the string based on <br/>
destinationCell.values = mytexts;

最新更新