NOTESSIDAN

Allt om Lotus Notes & Domino




Read values from a CSV file attached to a Document and present it in XPages
This simple server side javascript code read the content of a file stored in a rich text field
Scenario:
Read a csv file attached to a notes document and present it in Xpages without detaching the file to disc

This is what the attached CSV file looks like

Lev1;1001;Table;A
Lev2;1002;Chair;B
Lev3;1003;Sofa;C
Lev4;1004;Floor;D
and so on...



Step #1. Make sure you have an attachment in the first document, in the body field in a view call "Main"
Step #2. Put the following code in a link control in a new xpages

try{

var eo:NotesEmbeddedObject = null;

// Get the first document in the main view
var doc:NotesDocument = database.getView("Main").getFirstDocument()
var rt = doc.getFirstItem("Body")

// get the attachment in Body field
var v:java.util.Vector = rt.getEmbeddedObjects();
var it:java.util.Iterator = v.iterator();

// Create a new HashMap/TreeMap to store each line in
// TreeMap is better if you want the values sorted

//var hm:java.util.HashMap = new java.util.HashMap();
var hm:java.util.TreeMap = new java.util.TreeMap()
var c=0;

// Loop all attachments in body field
while (it.hasNext()) {
eo = it.next();

// Read the stream from the attachment
var br = new java.io.BufferedReader(java.io.BufferedReader(new java.io.InputStreamReader(eo.getInputStream())));
var strLine = "";

// Read each line in the csv file
while ((strLine = br.readLine()) != null) {

// put each line in the TreeMap
hm.put("Row " + c,strLine)
c++
}
br.close();
}

// return the TreeMap to the repeat control
viewScope.map = hm


}catch(e){
print(e)
}

Step #3. Add the following repeat control code to the xpage


<xp:repeat id="repeat1" rows="30" var="entry">
<xp:this.value><![CDATA[#{javascript:var m = viewScope.map
if(m!=null){
m.entrySet()
}
}]]></xp:this.value>
<xp:panel tagName="DIV">
<xp:text escape="true" id="computedField1" value="#{javascript:entry.getValue()}"></xp:text>
</xp:panel>
</xp:repeat>



Image below show what it looks like in the browser, each line in the csv file represent one entry in the repeat



If you only want the second value you can tweak the "entry" value like this
@Word(entry.getValue(),";",2)


<xp:repeat id="repeat1" rows="30" var="entry">
<xp:this.value><![CDATA[#{javascript:var m = viewScope.map
if(m!=null){
m.entrySet()
}
}]]></xp:this.value>
<xp:panel tagName="DIV">
<xp:text escape="true" id="computedField1"><xp:this.value><![CDATA[#{javascript:@Word(entry.getValue(),";",2)}]]></xp:this.value></xp:text>
</xp:panel>
</xp:repeat>



Image below show what only the second value






Many thanks to Paul Withers and Mark Leusink for their support



Relevanta Poster på Notessidan:



Fler Poster om:

Fler bloggar om:

Technorati:

Delicious:
Publicerad 2014-03-28 | Kommentarer: 3 | Läsningar:

Kommentarer

David Leedy ( 2014-03-29 00:18:16 )

Thomas Adrian ( 2014-03-31 08:19:38 )

Stephan H. Wissel ( 2014-04-01 09:36:43 )



Sök på Notessidan
Thomas Adrian
ThomasCertifierad utvecklare och administratör inom Notes & Domino

Kontakta mig







Senaste Sökningarna på Notessidan
Sön - sharepoint (R:1)
Sön - räkna and antal and besökare (R:0)
Sön - browser (R:17)
Sön - stoppar and mail (R:1)
Sön - korrupt (R:3)
Sön - böcker (R:0)
Sön - kalendern (R:12)
Sön - feature (R:15)
Sön - print and mime (R:2)
Sön - länka (R:0)
Sön - databasen (R:94)
Sön - access (R:47)
Sön - outlook (R:15)
Sön - webanvändare (R:0)
Sön - google and maps (R:6)
Sön - juffe (R:2)
Sön - uidoc.print (R:1)
Sön - java and lotusscript (R:26)
Sön - besök (R:0)




Databasstatistik Blogg
Uppdateras var 15:e Minut

Antal Poster:562
Antal Kommentarer: 1405
Alla sidvisningar idag:
Unika Besökare idag:
Databasstorlek: 153,25Mb
Fixup: 2019-09-22 21:33:25
FT:2019-09-23 07:19:27
DB Procent använd: 99,5
Antal Agenter: 42
Antal Formulär: 65
Antal Vyer: 60
Antal Dokument: 7983
Antal Image Resources: 108
Antal Subformulär: 18
Antal Scriptbibliotek: 11
Totalt Antal Designelement: 576
Antal Attachments: 159
Antal Konflikter: 0
Server version 1: 405
Server version 2: Release 9.0.1FP9|August 14, 2017
Server Plattform: Windows/64
Ändrad: 2019-09-23 09:09:43
Server Tidszon: Z=-1$DO=1$DL=3 -1 1 10 -1 1$ZX=131$ZN=W. Europe
Region:
Kommentarsspam Idag: 7
Kommentarsspam Totalt: 18902


BlogRankers.com

Top Sites at iWEBTOOL.com

Fight Spam! Click Here!

Bloggtoppen.se

Listed on BlogShares

Increase website traffic

Top Internet Blogs

Site Checker