最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - range.EntityValue doesn't seem to persist after function completes - Stack Overflow

programmeradmin1浏览0评论

I'm working on a custom Excel Add-in and want to use entityValue to create a behavior similar to the built-in Stocks data type (and documentation here: ). However, the entityValue doesn't seem to persist beyond the function where it's created - so when I go to read the entityValues it's undefined.

I have an ExtensionPoint for my Entity DataType defined in my manifest.xml (within the DesktopFormFactor):

    <ExtensionPoint xsi:type="DataType">
        <TypeName>TestData</TypeName>
        <Card>
            <Layout>
                <Section title="Test Details">
                    <Field label="Quantity" field="quantity" />
                    <Field label="Price" field="price" />
                </Section>
            </Layout>
        </Card>
    </ExtensionPoint>

Thus, all we're really capturing in this sample is quantity and price for a product.

Now, in a cell I put a product name (say, "Hammers") and run a command from the ribbon which converts that cell to an Entity of TestData:

async function createProductDataType(event) {
  try {
    await Excel.run(async (context) => {
        // Get the currently selected cell.
        const range = context.workbook.getSelectedRange();
        range.load(["address", "values", "entityValue"]);
        await context.sync();

        console.log("Converting cell to Product Data Type...");

        // Assume the user entered a product into the cell.
        const productID = range.values[0][0];

        console.log("Product ID: " + productID);

        // Populate additional details for the product (just for testing)
        const productData = {
            prdID: productID,
            quantity: 123,
            price: 2.95,
        };

        console.log("productData:" + JSON.stringify(productData, null, 2));

        // Just write the a version of productID to verify
        range.values = [["product type: " + productID]];

        console.log("Range Values: " + JSON.stringify(range.values, null, 2));

        // Create an object that conforms to the Excel.EntityValue interface.
        const productEntity = {
            entityType: "TestData", // matches the type defined in the manifest.
            prdID: productID, // A unique identifier for the entity instance.
            data: productData, // The detailed data for the entity.
        };

        // Assign productEntity to the entityValue property of the cell.
        range.entityValue = productEntity;

        // Update the cell's format to indicate it’s been converted.
        range.format.fill.color = "#88D3AF";

        range.load(["entityValue", "entityValue/entityType"]);
        await context.sync();

        // Verify the data in the cell
        console.log("Entity Value:" + JSON.stringify(range.entityValue, null, 2));
        console.log("Cell type: " + range.entityValue.entityType);

        await context.sync();
    });
  } catch (error) {
    console.log("Error in product Data Type creation: " + error);
  } finally {
    // Notify the host that the command function is complete.
    eventpleted();
  }
}

Invoking this function from the Ribbon behaves as expected. The console is updated as follows:

[Log] "Converting cell to Product Data Type..."               
[Log] "Product ID: Hammers"           
[Log] "productData:{\n  \"prdID\": \"Hammers\",\n  \"quantity\": 123,\n  \"price\": 2.95\n}"          
[Log] "Range Values:[\n  [\n    \"product type: Hammers\"\n  ]\n]"            
[Log] "Entity Value:{\n  \"entityType\": \"TestData\",\n  \"prdID\": \"Hammers\",\n  \"data\": {\n    \"prdID\": \"Hammers\",\n    \"quantity\": 123,\n    \"price\": 2.95\n  }\n}"           
[Log] "Cell type: TestData"  

So far, so (kind of) good. If I try to retrieve any data from another cell (e.g. =A2.quantity) it will return an error #FIELD! (and there was no change to the cell where we created this entity data type with a little icon).

However, to validate I created a second function that queries that cell to get the entity data: (function name irrelevant, this is just the code to do the check):

    // Get the currently selected cell.
    const range = context.workbook.getSelectedRange();
    range.load(["address", "values", "entityValue"]);
    await context.sync();

    console.log("Checking on entity data for cell from another function...");
    console.log("Value of CELL: " + range.values[0][0]);
    console.log("Entity Value CELL: " + JSON.stringify(range.entityValue, null, 2));

When I look at the console log for this, here is what I see:

[Log] "Checking on entity data for cell from another function..."             
[Log] "Value of CELL: product type: Hammers"          
[Log] "Entity Value CELL: undefined"  

So, it appears that the entityValue/Type didn't REALLY get set in the cell.

What am I doing wrong? (running the latest Beta of Excel, and I've set the requirements for the ExcelAPI version to 1.18).

发布评论

评论列表(0)

  1. 暂无评论