Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 操作员'<';无法应用于类型';编号';和';承诺<;无效>';使用Excel加载项_Javascript_Excel_Excel Addins - Fatal编程技术网

Javascript 操作员'<';无法应用于类型';编号';和';承诺<;无效>';使用Excel加载项

Javascript 操作员'<';无法应用于类型';编号';和';承诺<;无效>';使用Excel加载项,javascript,excel,excel-addins,Javascript,Excel,Excel Addins,我正在制作一个Excel外接程序,我想将Excel中的电子表格数据转换为Javascript中的2D数组,但我无法获取要转换的数据,我无法确定如何修复它,如何修复承诺,以便函数返回一个数字。getData函数也存在此问题 这位于Excel加载项的taskpane.js上。我已经尝试了.then(),在变量之前等待,然后让它运行,但是我无法使它工作 import { get } from "http"; /* * Copyright (c) Microsoft Corporation. All

我正在制作一个Excel外接程序,我想将Excel中的电子表格数据转换为Javascript中的2D数组,但我无法获取要转换的数据,我无法确定如何修复它,如何修复承诺,以便函数返回一个数字。getData函数也存在此问题

这位于Excel加载项的taskpane.js上。我已经尝试了.then(),在变量之前等待,然后让它运行,但是我无法使它工作

import { get } from "http";

/*
 * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
 * See LICENSE in the project root for license information.
 */

import { get } from "http";

/*
 * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
 * See LICENSE in the project root for license information.
 */

Office.onReady(info => {
  // Determine if the user's version of Office supports all the Office.js APIs that are used in the tutorial.
  if (!Office.context.requirements.isSetSupported('ExcelApi', '1.7')) {
    console.log('Sorry. The tutorial add-in uses Excel.js APIs that are not available in your version of Office.');
  }

  // Assign event handlers and other initialization logic.
  document.getElementById("run").onclick = run;
  if (info.host === Office.HostType.Excel) {
    document.getElementById("sideload-msg").style.display = "none";
    document.getElementById("app-body").style.display = "flex";
  }
});

function Create2DArray(rows) {
  var arr = [];

  for (var i = 0; i < rows; i++) {
    arr[i] = [];
  }

  return arr;
}

function sortedData() {
  var rows = getRowCount();
  var arrImport = getData();
  var arrExport = Create2DArray(rows);
  for (var r = 0; r < rows; r++) {
    arrExport[r][0] = arrImport[r][1];//money
    arrExport[r][1] = arrImport[r][5];//Company
    arrExport[r][2] = arrImport[r][0];//date
  }
  return arrExport;
}

async function getRowCount() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getActiveWorksheet();
    var rowCount = 0;

    for (var i = 0; i >= 0; i++) {
      var cell = sheet.getCell(i, 0);
      cell.load("address, values");
      await context.sync();
      if (cell.values[0][0] == "") {
        break;
      }
      else {
        rowCount++;
      }
    }
    await context.sync();
    return rowCount;
  });
}

async function getData() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getActiveWorksheet();
    //let rowCount = await getRowCount();
    var rowCount = getRowCount();
    var arrExport = Create2DArray(rowCount);

    for (var r = 0; r < rowCount; r++) {
      for (var c = 0; c < 5; c++) {
        var cell = sheet.getCell(r, c);
        cell.load("address, values");
        await context.sync();
        arrExport[r][c] = cell.values[0][0];
      }
    }
    await context.sync();
    return arrExport;
  })
}

async function run() {
  await Excel.run(async (context) => {
      var currentWorksheet = context.workbook.worksheets.getActiveWorksheet();
      rows = getRowCount();
  })
  .catch(function (error) {
      console.log("Error: " + error);
      if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
      }
  });
}
从“http”导入{get};
/*
*版权所有(c)微软公司。版权所有。根据麻省理工学院许可证授权。
*有关许可证信息,请参见项目根目录中的许可证。
*/
从“http”导入{get};
/*
*版权所有(c)微软公司。版权所有。根据麻省理工学院许可证授权。
*有关许可证信息,请参见项目根目录中的许可证。
*/
Office.onReady(信息=>{
//确定用户版本的Office是否支持教程中使用的所有Office.js API。
如果(!Office.context.requirements.isSetSupported('ExcelApi','1.7')){
console.log('抱歉,教程加载项使用的Excel.js API在您的Office版本中不可用');
}
//分配事件处理程序和其他初始化逻辑。
document.getElementById(“run”).onclick=run;
if(info.host==Office.HostType.Excel){
document.getElementById(“sideload msg”).style.display=“无”;
document.getElementById(“应用程序正文”).style.display=“flex”;
}
});
函数Create2DArray(行){
var-arr=[];
对于(变量i=0;i{
const sheet=context.workbook.worksheets.getActiveWorksheet();
var rowCount=0;
对于(变量i=0;i>=0;i++){
var cell=sheet.getCell(i,0);
单元负载(“地址、值”);
wait context.sync();
如果(单元格值[0][0]=“”){
打破
}
否则{
行计数++;
}
}
wait context.sync();
返回行计数;
});
}
异步函数getData(){
等待Excel.run(异步(上下文)=>{
const sheet=context.workbook.worksheets.getActiveWorksheet();
//让rowCount=等待getRowCount();
var rowCount=getRowCount();
var arexport=Create2DArray(rowCount);
对于(var r=0;r{
var currentWorksheet=context.workbook.worksheets.getActiveWorksheet();
rows=getRowCount();
})
.catch(函数(错误){
日志(“错误:+错误”);
if(OfficeExtension.error的错误实例){
log(“调试信息:+JSON.stringify(error.debugInfo));
}
});
}

我想从excel电子表格中获取所使用的行数,但无法使其正常工作,也无法计算异步。错误是标题。

第一个问题,因为getRowCount是异步的,所以它返回一个承诺

第二个问题,getRowCount实际上不返回任何内容

async function getRowCount() {
    let returnValue;
    await Excel.run(async(context) => {
        const sheet = context.workbook.worksheets.getActiveWorksheet();
        var rowCount = 0;

        for (var i = 0; i >= 0; i++) {
            var cell = sheet.getCell(i, 0);
            cell.load("address, values");
            await context.sync();
            if (cell.values[0][0] == "") {
                break;
            } else {
                rowCount++;
            }
        }
        await context.sync();
        returnValue = rowCount;
    });
    return returnValue;
}
可能有更好的方法来完成上述操作,但我不知道这个Excel.run等的工作原理,所以只要Excel.run返回一个承诺(我假设是因为您在上面使用了
wait
),那么上面的操作就应该有效

而且,在代码中的用法是

async function getData() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getActiveWorksheet();
    let rowCount = await getRowCount();
    var arrExport = Create2DArray(rowCount);

    for (var r = 0; r < rowCount; r++) {
    .
    .
    .
异步函数getData(){ 等待Excel.run(异步(上下文)=>{ const sheet=context.workbook.worksheets.getActiveWorksheet(); 让rowCount=等待getRowCount(); var arexport=Create2DArray(rowCount); 对于(var r=0;r
现在应该可以工作了

注释代码
let rowCount=await getRowCount()
是从异步函数得到结果的方式-使用它是它的一部分,但另一个问题是getRowCount没有返回语句。它有返回这个:运算符的…@Bravo'这是因为你的
异步函数getRowCount
实际上没有返回任何东西(在回调中返回某些内容不算在内)