Skip to content

Commit

Permalink
CFSpreadsheet contrib by @NathanStrutz. Thanks!
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Tuttle committed Dec 19, 2011
1 parent b849dc3 commit c8916de
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
67 changes: 67 additions & 0 deletions spreadsheet.cfc
@@ -0,0 +1,67 @@
<!-----------------------------------------------------------------------------------------------------------
MIT License
Copyright (c) 2011 The ColdFusion Community
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See also: http://www.opensource.org/licenses/mit-license.html
------------------------------------------------------------------------------------------------------------->
<cfcomponent>

<cfset variables.result = "" />

<cffunction access="public" returns="void" name="init">
<cfreturn this/>
</cffunction>

<cffunction name="readInfo">
<cfset arguments.action = "read" />
<cfset arguments.name = "variables.result" />
<cfset go(argumentcollection=arguments) />
<cfreturn variables.result />
</cffunction>

<cffunction name="readQuery">
<cfset arguments.action = "read" />
<cfset arguments.query = "variables.result" />
<cfset go(argumentcollection=arguments) />
<cfreturn variables.result />
</cffunction>

<cffunction name="update">
<cfset arguments.action = "update" />
<cfif structKeyExists(arguments, "query")>
<cfset variables.result = arguments.query />
<cfset arguments.query = "variables.result" />
</cfif>
<cfset go(argumentcollection=arguments) />
</cffunction>

<cffunction name="write">
<cfset arguments.action = "write" />
<cfif structKeyExists(arguments, "query")>
<cfset variables.result = arguments.query />
<cfset arguments.query = "variables.result" />
</cfif>
<cfset go(argumentcollection=arguments) />
</cffunction>

<cffunction name="go">
<cfspreadsheet attributeCollection="#arguments#" />
</cffunction>

</cfcomponent>
64 changes: 64 additions & 0 deletions tests/SpreadsheetTest.cfc
@@ -0,0 +1,64 @@
component extends="mxunit.framework.TestCase" {


function testSpreadsheet() {
var spreadsheet = createObject("component", "CFScript-Community-Components.spreadsheet");

var testFile = getTempDirectory() & "spreadsheet-unittest.xls";
if(fileExists(testFile)) {
fileDelete(testFile);
}

var dataToWrite = queryNew("ColumnA,ColumnB,ColumnC");
queryAddRow(dataToWrite, 3);
dataToWrite.ColumnA[1] = "One";
dataToWrite.ColumnB[1] = 1;
dataToWrite.ColumnC[1] = "Uno";
dataToWrite.ColumnA[2] = "Two";
dataToWrite.ColumnB[2] = 2;
dataToWrite.ColumnC[2] = "Dos";
dataToWrite.ColumnA[3] = "Three";
dataToWrite.ColumnB[3] = 3;
dataToWrite.ColumnC[3] = "Tres";

spreadsheet.write(
filename = testFile,
query = dataToWrite
);

var spreadsheetInfo = spreadsheet.readInfo(
src = testFile
);

assertTrue(structKeyExists(spreadsheetInfo, "summaryInfo"));
assertEquals(spreadsheetInfo.summaryInfo.sheets, 1);

spreadsheet.update(
filename = testfile,
query = dataToWrite,
sheetname = "blah"
);

var spreadsheetInfo = spreadsheet.readInfo(
src = testFile
);

assertTrue(structKeyExists(spreadsheetInfo, "summaryInfo"));
assertEquals(spreadsheetInfo.summaryInfo.sheets, 2);


var spreadsheetData = spreadsheet.readQuery(
src = testfile,
excludeHeaderRow = true,
headerRow = 1
);

assertTrue(isQuery(spreadsheetData));
assertEquals(spreadsheetData.ColumnA[3], "Three");

if(fileExists(testFile)) {
fileDelete(testFile);
}
}

}

0 comments on commit c8916de

Please sign in to comment.