【问题标题】:How to set multiple cell properties using Office Js?如何使用 Office Js 设置多个单元格属性?
【发布时间】:2020-07-07 08:39:39
【问题描述】:

我正在尝试使用 Excel.Range.setCellProperties() 为所选范围内的每个单元格设置单元格值和属性,但我在控制台中收到错误并且属性未加载到电子表格中。

这是我在控制台中遇到的错误,但我的单元格的属性数组似乎是正确的。

我之前从一个区域中提取了单元格值并将它们存储在一个二维字符串数组中:cellValues

这是cellValues 包含的示例

要设置的单元格属性之前也已从范围中提取并存储在名为cellStyles 的二维数组中。这是它包含的内容:

另外,这是我设置单元格属性的代码:

Excel.run(async function(ctx) {
  OfficeExtension.config.extendedErrorLogging = true;
  const activeRange = ctx.workbook.getSelectedRange();

  activeRange.values = cellValues;
  await ctx.sync();

  // This is where the error happens
  activeRange.setCellProperties(cellStyles);

  await ctx.sync();
}).catch(function(error) {
  console.log("Error: " + error);
  if (error instanceof OfficeExtension.Error) {
    console.log("Debug info: " + JSON.stringify(error.debugInfo));
  }
});

注意:为了让您更广泛地了解我正在做的事情,我将在 Excel 中导出所选范围的单元格值和格式属性,然后我希望能够“导入”这些值和单元格属性返回 Excel。

更新: 这是我用来从 Excel 导出内容的函数:

async function exportExcelRange() {
  let res = await Excel.run(async function(ctx) {
    const range = ctx.workbook.getSelectedRange();

    let cellPropsArray = range.getCellProperties({
      format: {
        autoIndent: true,
        borders: {
          color: true,
          weight: true,
          style: true,
          tintAndShade: true
        },
        fill: {
          color: true,
          patternColor: true,
          pattern: true,
          patternTintAndShade: true,
          tintAndShade: true
        },
        font: {
          bold: true,
          color: true,
          italic: true,
          name: true,
          size: true,
          strikethrough: true,
          subscript: true,
          superscript: true,
          tintAndShade: true,
          underline: true
        },
        horizontalAlignment: true,
        indentLevel: true,
        protection: true,
        readingOrder: true,
        shrinkToFit: true,
        textOrientation: true,
        useStandardHeight: true,
        useStandardWidth: true,
        verticalAlignment: true,
        wrapText: true
      },
      style: true,
      hyperlink: false
    });

    await ctx.sync();
    return cellPropsArray.value;
  }).catch(err => console.error(err));

  const nbCol = res[0].length;
  const nbRow = res.length;
  for (let row = 0; row < nbRow; row++) {
    for (let column = 0; column < nbCol; column++) {
      const cell = res[row][column];

      const newProps = {
        format: {
          autoIndent: cell.format.autoIndent,
          fill: {
            color: cell.format.fill.color,
            patternColor: cell.format.fill.patternColor,
            pattern: cell.format.fill.pattern,
            patternTintAndShade: cell.format.fill.patternTintAndShade,
            tintAndShade: cell.format.fill.tintAndShade
          },
          font: {
            bold: cell.format.font.bold,
            color: cell.format.font.color,
            italic: cell.format.font.italic,
            name: cell.format.font.name,
            size: cell.format.font.size,
            strikethrough: cell.format.font.strikethrough,
            subscript: cell.format.font.subscript,
            superscript: cell.format.font.superscript,
            tintAndShade: cell.format.font.tintAndShade,
            underline: cell.format.font.underline
          },
          horizontalAlignment: cell.format.horizontalAlignment,
          indentLevel: cell.format.indentLevel,
          readingOrder: cell.format.readingOrder,
          shrinkToFit: cell.format.shrinkToFit,
          textOrientation: cell.format.textOrientation,
          useStandardHeight: cell.format.useStandardHeight,
          useStandardWidth: cell.format.useStandardWidth,
          verticalAlignment: cell.format.verticalAlignment,
          wrapText: cell.format.wrapText
        },
        style: cell.style
      };

      res[row][column] = newProps;
    }
  }
  console.log(res);

  return JSON.stringify(res);
}
内容保存为 JSON 字符串,然后发送到我的应用程序的后端进行存储。 copyFrom() 函数本质上是我想要做的,但使用的是之前保存的 JSON 字符串。

【问题讨论】:

  • 能否分享一下导出单元格格式属性的代码?我想在我身边重现这个问题,如果你能分享我重现这个问题的要点,那就太好了。谢谢
  • @RaymondLu 我已经用所需的代码更新了我的问题,很遗憾我不能直接分享整个项目。

标签: javascript excel office-js


【解决方案1】:

我认为setCellPropertiesgetCellProperties 中的参数是不同的。 它们是 2 个不同的类 SettableCellPropertiesCellPropertiesLoadOptions,因此导出单元格格式属性并转换为 settableCellProperties 的逻辑对于您的方案至关重要。

但是,如果你只想复制范围内的格式,我建议你可以尝试使用range.copyFrom API,有一个copyFromType 选项,它只允许复制格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多