如何使用 Power Automate 中的文件内容在 excel 中添加完整图像
How to add a full image in excel using filecontent in power automate
我有一个 Microsoft 表单,可以让用户回答一些问题并上传图片。所有响应都将保存到 excel table 中,并带有自动流程。这些答案将保存为字符串,URL 指向 OneDrive,图片将保存到 excel table。
好的,很多人都问过如何使用 URL 将图片插入 excel 的问题,但到目前为止还没有解决方案。 Microsoft 的标准答案似乎不起作用——只要 URL 指向 OneDrive / SharePoint 就会出现获取错误(请参阅此 link)
我从另一个方面问同样的问题。我可以在 power automatic flow 中获取文件内容。当我下载文件内容并检查它时,它具有这样的数据结构(出于安全原因,这不是完整的文件内容):
{"statusCode":200,"headers":{"Pragma":"no-cache","x-ms-request-id":"0f368276-7f62-4daa-82a7-98f5fda","Timing-Allow-Origin":"*","x-ms-apihub-cached-response":"false","Accept-Ranges":"bytes","Cache-Control":"no-cache","Date":"Mon, 11 Oct 2021 09:21:21 GMT","ETag":""{D2E67548-99EE-4BA3-BAB3},4"","Location":"https://flow-apim-msmanaged-na-centralus-01.azure-apim.net/apim/onedriveforbusiness/a47f3c2699aa4df70f3614572a9/datasets/default/files/01YEFL272IOXTNF.../content?inferContentType=true","X-AspNet-Version":"4.0.30319","X-Powered-By":"ASP.NET","Content-Length":"137453","Content-Disposition":"attachment; filename="16337833927412488499783584031011.jpg"","Content-Type":"image/jpeg","Expires":"-1"},"body":{"$content-type":"image/jpeg","$content":"/9j/4UVORXhpZgAATU0AKgAAAAgADQEAAAMAAAABCrAAAAEBAAMAAAABDkAAAAECAAMAAAADAAAA6gEPAAIAAAAHAAAAqgEQAAIAAAAIAAAAsgESAAMAAAABAAAAAAEaAAUAAAABAAAAugEbAAUAAAABAAAAwgEoAAMAAAABAAIAAAExAAIAAAAfAAAAygEyAAIAAAAUAAAA8AITAAMAAAABAAEAAIdpAAQAAAABAAABBAAABLxIVUFXRUkAAExZQS1MMjkAAAAASAAAAAEAAABIAAAAAUxZQS1MMjkgMTEuMC4wLjE2MChDNjM2RTdSMlA0KQAAAAgACAAIMjAyMToxMDowOSAyMDo0MzozNwAALgENAAcAAAAAAAAAAIKaAAUAAAABAAADUoKdAAUAAAABAAAETogiAAMAAAABAAIAAIgnAAMAAAABA+gAAJAAAAcAAAAEMDIxMJADAAIAAAAUAAAEXpAEAAIAAAAUAAAEcpEBAAcAAAAEAQIDAJECAAUAAAABAAAERpIBAAoAAAABAAADWpICAAUAAAABAAAEVpIDAAoAAAABAAADQpIEAAoAAAABAAADSpIFAAUAAAABAAAEPpIHAAMAAAABAAUAAJIIAAMAAAABAAEAAJIJAAMAAAABAAAAAJIKAAUAAAABAAADMpJ8AAcAAABkAAADcJJ8AAcAAAAEJAAAAJJ8AAcAAABqAAAD1JJ8AAcAAAAIAAADYpJ8AAcAAAAFAAADapKQAAIAAAAHAAAEhpKRAAIAAAAHAAAEjpKSAAIAAAAHAAAElqAAAAcAAAAEMDEwMKABAAMAAAABAAEAAKACAAQAAAABAAAKsKADAAQAAAABAAAOQKAFAAQAAAABAAAEnqIXAAMAAAABAAIAAKMAAAcAAAABAwAAAKMBAAcAAAABAQAAAKQBAAMAAAABAAEAAKQCAAMAAAABAAAAAKQDAAMAAAABAAAAAKQEAAUAAAABAAADOqQFAAMAAAABABsAAKQGAAMAAAABAAAAAKQHAAMAAAABAAAAAKQIAAMAAAABAAAAAKQJAAMAAAABAAAAAKQKAAMAAAABAAAAAKQMAAMAAAABAAAAAAAAAAAAABXMAAAD6AAAAGQAAABkAAAAAAAAAAEAAAAAAAAACgJiWgA7msoAAASP3QAAJxAjIyoqbm9yAEF1dG8AACMjIyMKAAAArsgzAQiAAQAAAAAAAAAAAAAAAAABAAAAVAQAAP////////////////////////////////////////////////////////////////////////////////////...==}}
据我所知,“body”之后的字符串:{“$content-type”:“image/jpeg”,“$content”:,从 /9j/4UVORX 开始直到 ...= = 是该图像的 base64 字符串,逻辑上可以传递给 OfficeScript 并调用 worksheet.AddImage API 将完整图片作为图像直接插入 excel 工作表。
如果我将文件内容从 power automatic flow 传递给 OfficeScript,有人知道如何使用 OfficeScript 提取 base64 字符串吗?
===插件===
经过一番研究,我设法使用 json 函数从文件内容中提取了正确的字符串:
json(outputs('Get_file_content')?['body']?['$content'])
但是提示错误:Action 'Compose' failed...
哎呀...运行 没脑子...
这个流程对我有用:
Parse JSON
动作中的Content
表达式是这样的:
first(json(outputs('Get_response_details')?['body/r926aef8d3cf24b569e10513f3d540e0a']))
获取表单上传文件的信息。根据您的表单,实际表达式可能会有所不同。您只需将正确的表单回复字段插入 first(json( ))
:
Parse JSON
动作中使用的Schema
是:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"link": {
"type": "string"
},
"id": {
"type": "string"
},
"type": {},
"size": {
"type": "integer"
},
"referenceId": {
"type": "string"
},
"driveId": {
"type": "string"
},
"status": {
"type": "integer"
},
"uploadSessionUrl": {}
}
}
而用于传递到 Run script
操作的 base64ImageString
表达式是:
base64(outputs('Get_file_content')?['body'])
最后,如果您对我在这里使用的脚本感兴趣:
function main(workbook: ExcelScript.Workbook, sheetName: string, address: string, base64ImageString: string) {
let sheet = workbook.getWorksheet(sheetName);
let range = sheet.getRange(address);
let image = sheet.addImage(base64ImageString);
image.setTop(range.getTop());
image.setLeft(range.getLeft());
}
我有一个 Microsoft 表单,可以让用户回答一些问题并上传图片。所有响应都将保存到 excel table 中,并带有自动流程。这些答案将保存为字符串,URL 指向 OneDrive,图片将保存到 excel table。
好的,很多人都问过如何使用 URL 将图片插入 excel 的问题,但到目前为止还没有解决方案。 Microsoft 的标准答案似乎不起作用——只要 URL 指向 OneDrive / SharePoint 就会出现获取错误(请参阅此 link)
我从另一个方面问同样的问题。我可以在 power automatic flow 中获取文件内容。当我下载文件内容并检查它时,它具有这样的数据结构(出于安全原因,这不是完整的文件内容):
{"statusCode":200,"headers":{"Pragma":"no-cache","x-ms-request-id":"0f368276-7f62-4daa-82a7-98f5fda","Timing-Allow-Origin":"*","x-ms-apihub-cached-response":"false","Accept-Ranges":"bytes","Cache-Control":"no-cache","Date":"Mon, 11 Oct 2021 09:21:21 GMT","ETag":""{D2E67548-99EE-4BA3-BAB3},4"","Location":"https://flow-apim-msmanaged-na-centralus-01.azure-apim.net/apim/onedriveforbusiness/a47f3c2699aa4df70f3614572a9/datasets/default/files/01YEFL272IOXTNF.../content?inferContentType=true","X-AspNet-Version":"4.0.30319","X-Powered-By":"ASP.NET","Content-Length":"137453","Content-Disposition":"attachment; filename="16337833927412488499783584031011.jpg"","Content-Type":"image/jpeg","Expires":"-1"},"body":{"$content-type":"image/jpeg","$content":"/9j/4UVORXhpZgAATU0AKgAAAAgADQEAAAMAAAABCrAAAAEBAAMAAAABDkAAAAECAAMAAAADAAAA6gEPAAIAAAAHAAAAqgEQAAIAAAAIAAAAsgESAAMAAAABAAAAAAEaAAUAAAABAAAAugEbAAUAAAABAAAAwgEoAAMAAAABAAIAAAExAAIAAAAfAAAAygEyAAIAAAAUAAAA8AITAAMAAAABAAEAAIdpAAQAAAABAAABBAAABLxIVUFXRUkAAExZQS1MMjkAAAAASAAAAAEAAABIAAAAAUxZQS1MMjkgMTEuMC4wLjE2MChDNjM2RTdSMlA0KQAAAAgACAAIMjAyMToxMDowOSAyMDo0MzozNwAALgENAAcAAAAAAAAAAIKaAAUAAAABAAADUoKdAAUAAAABAAAETogiAAMAAAABAAIAAIgnAAMAAAABA+gAAJAAAAcAAAAEMDIxMJADAAIAAAAUAAAEXpAEAAIAAAAUAAAEcpEBAAcAAAAEAQIDAJECAAUAAAABAAAERpIBAAoAAAABAAADWpICAAUAAAABAAAEVpIDAAoAAAABAAADQpIEAAoAAAABAAADSpIFAAUAAAABAAAEPpIHAAMAAAABAAUAAJIIAAMAAAABAAEAAJIJAAMAAAABAAAAAJIKAAUAAAABAAADMpJ8AAcAAABkAAADcJJ8AAcAAAAEJAAAAJJ8AAcAAABqAAAD1JJ8AAcAAAAIAAADYpJ8AAcAAAAFAAADapKQAAIAAAAHAAAEhpKRAAIAAAAHAAAEjpKSAAIAAAAHAAAElqAAAAcAAAAEMDEwMKABAAMAAAABAAEAAKACAAQAAAABAAAKsKADAAQAAAABAAAOQKAFAAQAAAABAAAEnqIXAAMAAAABAAIAAKMAAAcAAAABAwAAAKMBAAcAAAABAQAAAKQBAAMAAAABAAEAAKQCAAMAAAABAAAAAKQDAAMAAAABAAAAAKQEAAUAAAABAAADOqQFAAMAAAABABsAAKQGAAMAAAABAAAAAKQHAAMAAAABAAAAAKQIAAMAAAABAAAAAKQJAAMAAAABAAAAAKQKAAMAAAABAAAAAKQMAAMAAAABAAAAAAAAAAAAABXMAAAD6AAAAGQAAABkAAAAAAAAAAEAAAAAAAAACgJiWgA7msoAAASP3QAAJxAjIyoqbm9yAEF1dG8AACMjIyMKAAAArsgzAQiAAQAAAAAAAAAAAAAAAAABAAAAVAQAAP////////////////////////////////////////////////////////////////////////////////////...==}}
据我所知,“body”之后的字符串:{“$content-type”:“image/jpeg”,“$content”:,从 /9j/4UVORX 开始直到 ...= = 是该图像的 base64 字符串,逻辑上可以传递给 OfficeScript 并调用 worksheet.AddImage API 将完整图片作为图像直接插入 excel 工作表。
如果我将文件内容从 power automatic flow 传递给 OfficeScript,有人知道如何使用 OfficeScript 提取 base64 字符串吗?
===插件===
经过一番研究,我设法使用 json 函数从文件内容中提取了正确的字符串:
json(outputs('Get_file_content')?['body']?['$content'])
但是提示错误:Action 'Compose' failed...
哎呀...运行 没脑子...
这个流程对我有用:
Parse JSON
动作中的Content
表达式是这样的:
first(json(outputs('Get_response_details')?['body/r926aef8d3cf24b569e10513f3d540e0a']))
获取表单上传文件的信息。根据您的表单,实际表达式可能会有所不同。您只需将正确的表单回复字段插入 first(json( ))
:
Parse JSON
动作中使用的Schema
是:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"link": {
"type": "string"
},
"id": {
"type": "string"
},
"type": {},
"size": {
"type": "integer"
},
"referenceId": {
"type": "string"
},
"driveId": {
"type": "string"
},
"status": {
"type": "integer"
},
"uploadSessionUrl": {}
}
}
而用于传递到 Run script
操作的 base64ImageString
表达式是:
base64(outputs('Get_file_content')?['body'])
最后,如果您对我在这里使用的脚本感兴趣:
function main(workbook: ExcelScript.Workbook, sheetName: string, address: string, base64ImageString: string) {
let sheet = workbook.getWorksheet(sheetName);
let range = sheet.getRange(address);
let image = sheet.addImage(base64ImageString);
image.setTop(range.getTop());
image.setLeft(range.getLeft());
}