comma in array element breaks spreadsheet

两盒软妹~` 提交于 2019-12-14 02:16:41

问题


When adding data to an array, one of the elements has commas in its value. An example of the value is "Trim marks at 103, 96, and 90".

Using the following code to add the array elements to the spreadsheet object, the partdescription element, as described above, has its data span multiple columns in the spreadsheet. It is handled as separate elements and not one.

<!---Create file name variable--->
    <cfset filenametouse = 'PartLevel_Report' />
    <!---Set directory and full file path--->
    <cfset theDir = GetDirectoryFromPath(GetCurrentTemplatePath()) /> 
    <!---Attach file extension to full file path and file name--->
    <cfset theFile = theDir & filenametouse & ".xls" /> 

    <cflock name="fileActionSentItems" type="exclusive" timeout="30" throwontimeout="true">
        <cfset SpreadsheetObj = spreadsheetNew()>
        <cfset fcol = {}>
        <cfset fcol.dataformat = "@">

        <!---Create the column headings--->
        <cfset SpreadsheetAddRow(SpreadsheetObj, "Part ##, Reorder ##, Description, Bin ##, Current Inv., Staged, Allocations, Available Inv., Shelf Count, Total Shipped, Total ## of Stores, Total Ordered, Avg. Per Store, Lead Time (in days), Low Water Mark, Total ## of Stores Remaining")>

            <cfoutput query="getParts" group="partnum">

    <cfset aColumns = [ partnum , shortchar08 , partdescription , binlist , inventory.currinv , staged.stagedqty , alloc.allocqty , available , shelfCount , shipdtl.shipqty , getNumberofStores.numStores , tordered , APS, paddedLeadTime, LWM , storesRemain] />

    <!---add the column data to the spreadsheet--->
    <cfset SpreadsheetAddRow(SpreadsheetObj, ArrayToList(aColumns)) />
</cfoutput>

        <!---Generate the spreadsheet--->
        <cfspreadsheet action="write" filename="#theFile#" name="SpreadsheetObj" sheetname="Sheet1" overwrite="true" />

How may I alleviate this issue?

Solved: I set a variable to the partdescription with all commas replaced with semicolons. Now the data appears all in the same column:

 <cfset cleanDesc = rereplace(partdescription, ",", ";", "all")>
    <cfset aColumns = [ partnum , shortchar08 , cleanDesc , binlist , inventory.currinv , staged.stagedqty , alloc.allocqty , available , shelfCount , shipdtl.shipqty , getNumberofStores.numStores , tordered , APS, paddedLeadTime, LWM , storesRemain] />

回答1:


Have you tried SpreadsheetAddRows() instead? Pass in the whole query? Maybe that will be a workaround assuming that SpreadsheetAddRows() doesn't just call SpreadsheetAddRow() underneath.

Since SpreadsheetAddRow() does not accept an optional delimiter that most CF list feature supports, I guess alternatively you can escape the comma into something else, then replace it as a comma using spreadsheetsetcellvalue()?

You may want to file a feature request to Adobe for an optional delimiter for the SpreadsheetAddRow() function.




回答2:


try using "listQualify()" to first insure that your list elements - the ones that contain commas - do not mess with the list length.




回答3:


According to the Adobe Engineer off Dan A.'s link

Kunal from Adobe has pointed out a much more straightforward workaround, which is to wrap the variable in single quotes and then in double quotes

So... right after <cfset aColumns = [ ... ]>

<!--- wrap element with single quote if it contains a comma --->
<cfloop from="1" to="#arrayLen(aColumns)#" index="i">
    <cfif aColumns[i] CONTAINS ','>
        <cfset aColumns[i] = "'#aColumns[i]#'">
    </cfif>
</cfloop>

<cfset SpreadsheetAddRow(SpreadsheetObj, ArrayToList(aColumns)) />



回答4:


As Henry said, wrapping the variable in single quotes worked for me, except when the last character is a comma.

<cfset SpreadsheetAddRow(SpreadsheetObj, "'col1,','col2'" />

So I added a space to the value of each cell, like this:

<cfset SpreadsheetAddRow(SpreadsheetObj, "'col1, ','col2 '" />


来源:https://stackoverflow.com/questions/9522735/comma-in-array-element-breaks-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!