代码之家  ›  专栏  ›  技术社区  ›  Andrew Hall

Excel Office。js-图表。Mac问题上的getImage()

  •  0
  • Andrew Hall  · 技术社区  · 7 年前

    我使用 chartObject.getImage() 获取Excel图表的图像,以便它们可以在Word和PowerPoint中显示。我已经做了几个月了,它在Office for Windows上运行得很好。我最近(通过客户投诉)发现,在Excel for Mac上执行getImage时存在问题。请参见下面的典型示例(我添加了黑色边框)。

    Shows offset issue on Mac

    Excel for Mac中的getImage()似乎总是偏移图表图像。通常,图表根本不可见(图像全部为白色)。

    我尝试过更改高度、宽度和装配模式(Fit、FitAndCenter、Fill)参数,但似乎没有任何方法可以解决此问题。

    您还可以使用 Store add-in .

    这是已知问题吗?有办法解决吗?

    要复制:

    在Script Lab的Sheet1中,添加任何“图表1”。

    图表离图纸左上角越远,图像的偏移量越大。因此,如果你把图像放在f20附近,那么你只能看到图表的右下角。

    $("#run").click(run);
    function run(){
    Excel.run(function (ctx) {
        var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart 1");
        var image = chart.getImage();
        return ctx.sync().then(function () {        
            var srcImage = "data:image/png;base64, " + image.value;
            $("#chart").attr("src", srcImage);
        });
    }).catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
    }
    
    
    
    <p class="ms-font-m">Shows the chart image below</p>
    <button id="run" class="ms-Button">
        <span class="ms-Button-label">Run code</span>
    </button><br/>
    <img id="chart" src="" alt="" />
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrew Hall    7 年前

    该问题已在Mac Excel版本16.10中修复