首页 > 解决方案 > 如何
使用 Javascript Excel API 用换行符替换 XML

问题描述

我有一个电子表格,其中包含多个包含要删除的 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
    })
  })
}

假设我的 sourceCell 包含以下文本:

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

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

标签: javascriptexceloffice-jsoffice-addins

解决方案


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

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

推荐阅读