Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Office.js从Excel中的多个NamedItem获取格式?_Javascript_Excel_Office Js - Fatal编程技术网

Javascript 如何使用Office.js从Excel中的多个NamedItem获取格式?

Javascript 如何使用Office.js从Excel中的多个NamedItem获取格式?,javascript,excel,office-js,Javascript,Excel,Office Js,我试图从NamedItems列表中检索一些格式信息。基本上,我的电子表格有几个命名范围分布在多个工作表上,我想获得一些格式化信息,比如每个命名范围的字体大小 这就是我到目前为止所做的: async function getRangesAndJson() { // 1. Find all prz ranges in file let myRanges = await Excel.run(async ctx => { const namedRangesInFile = ctx.

我试图从NamedItems列表中检索一些格式信息。基本上,我的电子表格有几个命名范围分布在多个工作表上,我想获得一些格式化信息,比如每个命名范围的字体大小

这就是我到目前为止所做的:

async function getRangesAndJson() {
  // 1. Find all prz ranges in file
  let myRanges = await Excel.run(async ctx => {
    const namedRangesInFile = ctx.workbook.names;
    namedRangesInFile.load("items");
    await ctx.sync();
    return namedRangesInFile.items.filter(range => range.name.startsWith("abc_"));
  }).catch(error => {
    console.log("error: " + error);
    if (error instanceof OfficeExtension.Error) {
      console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
  });

  // 2. Extract their formatting in JSON, one by one
  await Excel.run(async function(ctx) {
    for (let index = 0; index < myRanges.length; index++) {
      const namedRange = myRanges[index];
      const range = namedRange.getRange();
    }
    await ctx.sync();
  }).catch(error => {
    console.log("error: " + error);
    if (error instanceof OfficeExtension.Error) {
      console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
  });
}
异步函数getRangesAndJson(){ //1.在文件中查找所有prz范围 让myRanges=等待Excel.run(异步ctx=>{ const namedRangesInFile=ctx.workbook.names; namedRangesInFile.load(“项目”); 等待ctx.sync(); 返回namedRangesInFile.items.filter(range=>range.name.startsWith(“abc”); }).catch(错误=>{ 日志(“错误:+错误”); if(OfficeExtension.error的错误实例){ log(“调试信息:+JSON.stringify(error.debugInfo)); } }); //2.逐个提取JSON格式 等待Excel.run(异步函数(ctx){ for(让index=0;index{ 日志(“错误:+错误”); if(OfficeExtension.error的错误实例){ log(“调试信息:+JSON.stringify(error.debugInfo)); } }); } 我不知道怎么做,尤其是因为这个操作需要很多循环。
实现这一点的最佳方法是什么?

在代码中,您可以从每个命名项中获取范围,如果该范围的字体相同,您可以使用range.format.font从该范围中获取字体

  const range = namedRange.getRange();
  range.load(["format/*", "format/fill", "format/borders", "format/font"]);
  await ctx.sync();
  console.log(range.format.font.name);
如果要获取此范围内每个单元格的字体名称,可能需要使用
范围。getCellProperties
,请参阅


顺便说一句,从中,我们建议不要将context.sync放在循环中。

在代码中,您可以从每个命名项中获取范围,如果该范围的字体相同,您可以使用range.format.font从该范围中获取字体

  const range = namedRange.getRange();
  range.load(["format/*", "format/fill", "format/borders", "format/font"]);
  await ctx.sync();
  console.log(range.format.font.name);
如果要获取此范围内每个单元格的字体名称,可能需要使用
范围。getCellProperties
,请参阅


顺便说一句,从中,我们建议不要将context.sync放入循环。

感谢您的输入,不幸的是,这并不能解决我的问题。在for循环中调用await Excel.run(…)会更好吗?根据我对问题描述的理解,我认为问题在于获取每个namedItem中范围的格式(字体大小)。那么,你是说我的示例代码无法获得范围的字体大小吗?感谢您的输入,不幸的是,这并不能解决我的问题。在for循环中调用await Excel.run(…)会更好吗?根据我对问题描述的理解,我认为问题在于获取每个namedItem中范围的格式(字体大小)。那么,您的意思是我的示例代码无法获取范围的字体大小吗?