我是微软Dynamics 365 & Power Platform方面的工程师/顾问罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),这是我的第488篇原创文章,写于2023年2月22日。
之前我写过一篇用Flow处理Excel数据文章如下:
因为都是自己准备的Excel格式,所以没有碰到表格列标题中有换行的。在实际项目中可能碰到这种情况,我今天来记录下怎么解决这个问题。首先我准备了一个简单的Excel,表格及表格中的数据如下:

我将这个Excel上传到Sharepoint Online中,然后我新建一个Instant类型的Cloud flow来测试读取Excel中的内容。

Trigger的话我选择Manually trigger a flow,方便测试。

我添加一个 Excel Online (Business) 这个Connector下面的

添加的步骤如下,保存并进行测试。

运行成功后我点击 Click to download 链接

读取到的内容如下:
{
"statusCode":200,
"headers":{
"Pragma":"no-cache",
"Transfer-Encoding":"chunked",
"Vary":"Accept-Encoding",
"x-ms-request-id":"aa9f4700-5eed-43d1-8140-2ee80c4b4334;0b42028d-d998-47b4-b76a-fc123f298e65;941dc3bf-278c-41d4-9075-95675b4bbb17;1fcd18a4-3faa-410c-a3c8-edae490d384f",
"OData-Version":"4.0",
"Strict-Transport-Security":"max-age=31536000; includeSubDomains",
"X-Content-Type-Options":"nosniff",
"X-Frame-Options":"DENY",
"Timing-Allow-Origin":"*",
"x-ms-apihub-cached-response":"true",
"x-ms-apihub-obo":"false",
"Cache-Control":"no-store, no-cache",
"Date":"Wed, 22 Feb 2023 00:09:40 GMT",
"Content-Type":"application/json; odata.metadata=minimal; odata.streaming=true",
"Expires":"-1",
"Content-Length":"758"
},
"body":{
"@odata.context":"https://excelonline-ea.azconn-ea.p.azurewebsites.net/$metadata#drives('b%21TNPruMBLaECRPaeF_zA-glm81U4vQ2FIgre-76_-Lf06enr3xTnuTrtMrX7nt9tL')/Files('014Q626LAHUJMD65LHRBCIDIOEVBCBM2KL')/Tables('%7B71957003-0B1A-4622-8CFC-B0808871FF32%7D')/items",
"value":[
{
"@odata.etag":"",
"ItemInternalId":"a1b73a4e-a6d9-449d-9811-0abffcd9abf6",
"序号":"1",
"姓名":"罗勇",
"籍贯\n(出生地)":"湖南省衡阳市",
"年龄":" "
},
{
"@odata.etag":"",
"ItemInternalId":"c0fdb874-74ae-435e-bbb3-766e5f1d33a6",
"序号":"2",
"姓名":"罗格",
"籍贯\n(出生地)":"湖南省衡阳市",
"年龄":"14"
}
]
}
}
可以看到读出来的带换行的列标题是 籍贯\n(出生地) ,换行符替换成了 \n 。还可以看到第一行的年龄列包括一个空格。
下面我增加一个 Data Operation这个类别下的 Select这个操作步骤,将读取到的数据转换为JSON数组,也用英文作为元素名。

From我选择读取Excel数据中的value值。

其他几个列我也做些映射

籍贯列可以看到展示的tooltip显示有换行是
item()?['籍贯
(出生地)']

如果我们通过Peek code来看适用的表达式是:item()?['籍贯\n(出生地)']

对于年龄列,我想转换为数字,所以我适用了表达式 float(item()?['年龄']) ,设置好后的这个步骤如下:

然后运行一下报错了,如下:

报错的文本是:
The execution of template action 'Select' failed: The evaluation of 'query' action 'where' expression '{
"Seq": "@item()?['序号']",
"FullName": "@item()?['姓名']",
"Hometown": "@item()?['籍贯\n(出生地)']",
"Age": "@float(item()?['年龄'])"
}' failed: 'The template language function 'float' was invoked with a parameter that is not valid. The value cannot be converted to the target type.'.
比较容易知道是第一行的空格转换为float报错了,如果为空的话,我们转换float他会报错,那么我们改下表达式为 if(empty(item()?['年龄']),0,float(item()?['年龄'])) 能解决问题嘛?答案是不能,还是同样的报错信息,因为空格不是empty,那怎么办?我们改成 if(empty(trim(item()?['年龄'])),0,float(item()?['年龄'])) 就可以解决这个问题了,也就是加上 trim 函数来去掉前后的空格就可以了。当然如果为空不想转换为0,而是null也可以的,表达式改成 if(empty(trim(item()?['年龄'])),null,float(item()?['年龄'])) 即可。

今天测试时候没有发现列标题有换行导致问题,可能是产品解决了这个问题,假如有问题如何解决?
点击旁边的 Switch Map to text mode 图标。

将有问题的那个列的表达式删除重新填写成之前自动生成的(比如我这里是
item()?['籍贯\n(出生地)'] ,注意换行符替换为 \n )即可,当然最好切换回去之前的Map模式。
