从任务窗格复制图像到Excel – Office-JS

huangapple go评论107阅读模式
英文:

Copy Image from Taskpane to Excel - Office-JS

问题

I understand that you want a translation of the provided text. Here is the translation:

我正在通过Excel附加组件制作条形码。到目前为止,我已经在TaskPane中使用https://github.com/lindell/JsBarcode工作来生成条形码,但我想将其放到工作表上。

我的HTML知识有限,这是我正在使用的标签,但如果需要,我认为我可以使用其他标签,GitHub中提供了使用Canvas/svg等的示例。

<img id="barcode" />

到目前为止,这是我得到的内容,我没有收到错误,但它不起作用。

await JsBarcode("#barcode", "Hi!");

const data = {};
JsBarcode(data, 'text')
console.log('data:')
console.log(data)

var ws = context.workbook.worksheets.getActiveWorksheet();
ws.getRange("A1").values = data

这是对象data

[object Object]
   {
      [functions]: ,
      __proto__: { },
      encodings: [
         0: {
            [functions]: ,
            __proto__: { },
            data: "1101001000010011110100101100100001111001001010011110100111101011101100011101011",
            options: {
               [functions]: ,
               __proto__: { },
               background: "#ffffff",
               displayValue: true,
               font: "monospace",
               fontOptions: "",
               fontSize: 20,
               format: "CODE128",
               height: 100,
               lineColor: "#000000",
               margin: 10,
               marginBottom: 10,
               marginLeft: 10,
               marginRight: 10,
               marginTop: 10,
               Symbol()_7.6h76ic49xap: undefined,
               Symbol(nodejs.util.inspect.custom)_j.6h76ic49xdy: undefined,
               text: undefined,
               textAlign: "center",
               textMargin: 2,
               textPosition: "bottom",
               width: 2
            },
            Symbol()_7.6h76ic49xap: undefined,
            Symbol(nodejs.util.inspect.custom)_j.6h76ic49xdy: undefined,
            text: "text"
         },
         length: 1
      ],
      Symbol()_7.6h76ic49xap: undefined,
      Symbol(nodejs.util.inspect.custom)_j.6h76ic49xdy: undefined
   }

更新:我需要解决方案在HTTP下可用,因为这是我当前加载附加组件的方式,由于Excel与公司SSL证书存在问题。我已经研究了使用新的剪贴板API,但它仅在HTTPS下可用。

英文:

I'm working on making barcodes via an Excel Add-In. So far, I've got https://github.com/lindell/JsBarcode working to generate a barcode inside the TaskPane, but I'd like to get this onto the Worksheet.

My HTML knowledge is somewhat limited, this is the tag I'm using, but I think I could use other tags if needed, examples offered in github using Canvas/svg etc.

<img id="barcode" />

So far this is what I've got, I get no errors, but it doesn't work.

await JsBarcode("#barcode", "Hi!");

const data = {};
JsBarcode(data, 'text') //, { ...options });
console.log('data:')
console.log(data)

var ws = context.workbook.worksheets.getActiveWorksheet();
ws.getRange("A1").values = data

Here is the Object data:

[object Object]
   {
      [functions]: ,
      __proto__: { },
      encodings: [
         0: {
            [functions]: ,
            __proto__: { },
            data: "1101001000010011110100101100100001111001001010011110100111101011101100011101011",
            options: {
               [functions]: ,
               __proto__: { },
               background: "#ffffff",
               displayValue: true,
               font: "monospace",
               fontOptions: "",
               fontSize: 20,
               format: "CODE128",
               height: 100,
               lineColor: "#000000",
               margin: 10,
               marginBottom: 10,
               marginLeft: 10,
               marginRight: 10,
               marginTop: 10,
               Symbol()_7.6h76ic49xap: undefined,
               Symbol(nodejs.util.inspect.custom)_j.6h76ic49xdy: undefined,
               text: undefined,
               textAlign: "center",
               textMargin: 2,
               textPosition: "bottom",
               width: 2
            },
            Symbol()_7.6h76ic49xap: undefined,
            Symbol(nodejs.util.inspect.custom)_j.6h76ic49xdy: undefined,
            text: "text"
         },
         length: 1
      ],
      Symbol()_7.6h76ic49xap: undefined,
      Symbol(nodejs.util.inspect.custom)_j.6h76ic49xdy: undefined
   }

从任务窗格复制图像到Excel – Office-JS

Update: I need the solution to be available under HTTP as that is how I currently load my Add-In due to Excel having issues with my company SSL Certificate. I've looked/read into using new Clipboard API, but its only available under HTTPS.

答案1

得分: 2

条形码或二维码生成器允许将生成的图形代码导出为图像。在从Excel要求集1.9开始的Office Web加载项中,您可以使用[addImage][1]方法来插入图像:

addImage(base64ImageString: string): Excel.Shape

例如,您可以使用以下代码:

Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("MyWorksheet");
    let cell = sheet.getRange("D5");
    cell.load('top,left') //预加载顶部和左侧
    let myBase64 = "在此处放入您的base64字符串";
    const shape = sheet.shapes.addImage(myBase64)

    await context.sync()

    shape.incrementLeft(cell.left) // <- 左侧
    shape.incrementTop(cell.top) // <- 顶部
    await context.sync();
})
英文:

Bar-code or qr-code generators allow exporting the generated graphical code as image. In Office web add-ins starting from Excel requirement set 1.9 you can use the addImage method to insert images:

addImage(base64ImageString: string): Excel.Shape

For example, you may use the following code:

Excel.run(async (context) =&gt; {
    let sheet = context.workbook.worksheets.getItem(&quot;MyWorksheet&quot;);
    let cell = sheet.getRange(&quot;D5&quot;)
    cell.load(&#39;top,left&#39;) //pre-load top and left
    let myBase64 = &quot;your bas64string here&quot;;
    const shape = sheet.shapes.addImage(myBase64)

    await context.sync()

    shape.incrementLeft(cell.left) // &lt;- left
    shape.incrementTop(cell.top) // &lt;-top
    await context.sync();
})

答案2

得分: 0

基本上,我必须将图像转换为Base64,然后将其插入到工作表中。

function textToBase64Barcode(text) {
    var canvas = document.createElement("canvas");
    JsBarcode(canvas, text, { format: "CODE39" });
    return canvas.toDataURL("image/png");
}

export async function Button_BARCODES_Run_OnClick() {
    try {
        await Excel.run(async (context) => {
            //开始
            console.log('Button_BARCODES_Run_OnClick')
            var Base64Image = await textToBase64Barcode('text')
            Base64Image = Base64Image.split(',')[1]
            console.log('Base64Image:')
            console.log(Base64Image)
            
            var ws = context.workbook.worksheets.getActiveWorksheet();
            ws.getRange("A1").select()
            await context.sync()
            await IMAGES.Insert_Image_At_Selection(context, Base64Image)
            //data.encodings.data
            
            //结束
            await context.sync();
        });
    } catch (error) {
        console.error(error);
    }
}

IMAGES.Insert_Image_At_Selection = async function Insert_Image_At_Selection(context,Image_Base64_Str) {
    //作为参考保留,但使用 "shapes"
    Office.context.document.setSelectedDataAsync(Image_Base64_Str, {
        coercionType: Office.CoercionType.Image,
        imageLeft: 50,
        imageTop: 50,
        imageWidth: 100,
        imageHeight: 100
    },
        function (asyncResult) {
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                console.log("Action failed with error: " + asyncResult.error.message);
            }
        });
}
英文:

Basically, I had to convert the image to Base64 then insert that into Worksheet.

https://stackoverflow.com/a/35703395/5079799

function textToBase64Barcode(text) {
var canvas = document.createElement(&quot;canvas&quot;);
JsBarcode(canvas, text, { format: &quot;CODE39&quot; });
return canvas.toDataURL(&quot;image/png&quot;);
}

export async function Button_BARCODES_Run_OnClick() {
try {
await Excel.run(async (context) =&gt; {
//START
console.log(&#39;Button_BARCODES_Run_OnClick&#39;)
var Base64Image = await textToBase64Barcode(&#39;text&#39;)
Base64Image = Base64Image.split(&#39;,&#39;)[1]
console.log(&#39;Base64Image:&#39;)
console.log(Base64Image)
var ws = context.workbook.worksheets.getActiveWorksheet();
ws.getRange(&quot;A1&quot;).select()
await context.sync()
await IMAGES.Insert_Image_At_Selection(context, Base64Image)
//data.encodings.data
//END
await context.sync();
});
} catch (error) {
console.error(error);
}
}

IMAGES.Insert_Image_At_Selection = async function Insert_Image_At_Selection(context,Image_Base64_Str) {
//Leaving as reference, but use &quot;shapes&quot;
Office.context.document.setSelectedDataAsync(Image_Base64_Str, {
coercionType: Office.CoercionType.Image,
imageLeft: 50,
imageTop: 50,
imageWidth: 100,
imageHeight: 100
},
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(&quot;Action failed with error: &quot; + asyncResult.error.message);
}
});
}

huangapple
  • 本文由 发表于 2023年4月20日 03:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定