首页 > 解决方案 > excel-JS excel left()工作表功能不起作用

问题描述

当我尝试在 Visual Studio Excel-JS api 中使用 Excel left() 工作表函数时,它根本不起作用!这意味着没有错误消息,没有价值,也没有线索什么不起作用我在说这个语句:

  onzin = ctx.workbook.functions.left(Acode.values.toString(), 7);
  onzin.load("values","address");

根据 Microsoft https://docs.microsoft.com/en-us/javascript/api/excel/excel.worksheetcollection?view=office-js的(非常不完整的)文档, 您必须在 Excel 对象模型中加载变量在使用它之前和上下文同步之后,这些值是可用的。但是在我的第二个承诺中,我加载了“onzin”的值和地址,但是当我想将值分配给 ICcode Range 时,它​​没有这样做,此外,当我在其中放置断点时,它似乎是空的并且没有错误。

这是我的代码:

Excel.run(function (ctx) {
//always use the Data sheet
    var MyDataSheet = ctx.workbook.worksheets.getItem("Data");
    var ConfigSheet = ctx.workbook.worksheets.getItem("Config");
    var onzin;
    MyDataSheet.activate();
    var productStartRange = MyDataSheet.getUsedRange();
    //define a range before the values can be loaded to the Excel Object model
    var Acode = MyDataSheet.getRange("A3").load("values, address");
productStartRange.load("values, address, length");                                                                
// Run the queued-up commands, and return a promise to indicate task completion                       
return ctx.sync()
    .then(function () {
        var myBounds = GetBounds(productStartRange);                                
        ConfigSheet.activate();

        //put the column and row bounds in the config sheet                            
        var ColBounds = ConfigSheet.getRange("B22");
        var RowBounds = ConfigSheet.getRange("B21");
        var NumProducts = ConfigSheet.getRange("B34");
        var NumProperties = ConfigSheet.getRange("B27");

        //ICcode.values = ctx.workbook.functions.left(Acode.values.toString(), 7);                                
        onzin = ctx.workbook.functions.left(Acode.values.toString(), 7);
        onzin.load("values","address");

        //ICcode.values = Acode.values.toString().substring(0, 7);
        //ICcode.values = onzin.values;
        ColBounds.values = myBounds.LastCol;
        RowBounds.values = myBounds.LastRow - 1;

        //total number of products
        NumProducts.values = RowBounds.values - 2;
        //total number of properties                                
        NumProperties.values = ColBounds.values - 2;

        //load the products  from the Data source sheet into one range
        var ProductRange = MyDataSheet.getRangeByIndexes(3, 1, myBounds.LastRow, 3);
        ProductRange.load("values");

    })
    .then(ctx.sync)
    .then(function () {
        var ICcode = ConfigSheet.getRange("B36");
        ICcode = onzin.values;

        //var Mystring = rowAddress.address;
        showNotification("onzin waardes: ", onzin.values);
        var PropSheet = ctx.workbook.worksheets.getItem("PropertySelection");
        PropSheet.activate();

    }); 


}).catch(errorHandler);

我希望工作表函数从“Acode”的单元格值中获取前 7 个字符,并将其写入位置 B36 上的范围 ICcode。

任何帮助,将不胜感激

标签: javascriptexceloffice-js

解决方案


删除了我之前的答案,因为我误读了您的部分代码。这行代码有两个问题:

onzin.load("values","address");
  1. 第一个字符串上不应有“s”。这只是“价值”。此外,从行中删除“s” ICcode = onzin.values;
  2. onzin 对象上没有“地址”属性。(Excel.Range 对象确实具有名为 values 和 address 的属性,这就是为什么我在原始答案中认为您将 onzin 视为 Excel.Range 对象的原因。)

通过阅读官方文档中的这篇文章,我能够发现错误:调用内置 Excel 工作表函数。如果您搜索“office 加载项工作表功能”,本文是 Bing 和 Google 中的第一个搜索结果。因此,我不太同意您的观点,即文档“非常不完整”。


推荐阅读