Need help or advice ? Come to the Icy club ! - Every Wenesday morning from 9h30 to 12h30 - Francois Jacob Building - Main hall - Pasteur
Register

Merge XLS files

by stef / Stephane Dallongeville

Take an input folder containing several XLS files and merge them together while adding a new column containing XLS file name (presumably image name).

version 2 (last version):

download to use and modify in Icy. How do I use scripts ? A Problem ? ask the community.
Changelog for this version: Keep cell number format when possible.
importClass(Packages.icy.util.StringUtil)
importClass(Packages.icy.util.XLSUtil)
importClass(Packages.icy.gui.dialog.ConfirmDialog)
importClass(Packages.icy.file.FileUtil)
importClass(Packages.plugins.tprovoost.scripteditor.uitools.filedialogs.FileDialog)


d = new FileDialog()

// get source folder
srcFolder = d.openFolder()
if (srcFolder == null) throw "Operation canceled"

// get output file
outFile = d.open()
if (outFile == null) throw "Operation canceled"

if (outFile.exists())
{
	if (!ConfirmDialog.confirm("Output file already exists, continue ?"))
		throw "Operation canceled"

	// delete output file
	outFile.delete();
}

mergedDoc = XLSUtil.createWorkbook(outFile)
mergedSh = XLSUtil.createNewPage(mergedDoc, "merged results")

files = FileUtil.getFiles(srcFolder, null, false, false, false)
mergedY = 0;

for(f = 0; f < files.length; f++)
{
	file = files[f]

	// ignore invalid entries
	if (!file.exists()) continue;

	println("Processing file: " + file.getName())

	doc = XLSUtil.loadWorkbookForRead(file)
	sh = doc.getSheet(0)

	// first file ?
	if (f == 0) XLSUtil.setCellString(mergedSh, 0, 0, "Image")
	
	for(y = 0; y < sh.getRows(); y++)
	{		
		// write image name in first column
		if (y > 0) XLSUtil.setCellString(mergedSh, 0, mergedY, file.getName())
		
		for(x = 0; x < sh.getColumns(); x++)
		{
			cell = sh.getCell(x, y)
			// not first row or first file
			if ((y > 0) || (f == 0))
			{
				type = cell.getType()
				
				if (type == type.NUMBER)
				{
					d = StringUtil.parseDouble(cell.getContents(), 0)
					XLSUtil.setCellNumber(mergedSh, x + 1, mergedY, d)
				}
				else XLSUtil.setCellString(mergedSh, x + 1, mergedY, cell.getContents())
			}
		}

		// pass to next row
		if ((y > 0) || (f == 0)) mergedY++;
	}
}

XLSUtil.saveAndClose(mergedDoc)

This script has no documentation. A Problem ? ask the community.


Previous versions:


version 1:

download to use and modify in Icy.
Changelog for this version: initial version
importClass(Packages.icy.util.XLSUtil)
importClass(Packages.icy.gui.dialog.ConfirmDialog)
importClass(Packages.icy.file.FileUtil)
importClass(Packages.plugins.tprovoost.scripteditor.uitools.filedialogs.FileDialog)


d = new FileDialog()

// get source folder
srcFolder = d.openFolder()
if (srcFolder == null) throw "Operation canceled"

// get output file
outFile = d.open()
if (outFile == null) throw "Operation canceled"

if (outFile.exists())
{
	if (!ConfirmDialog.confirm("Output file already exists, continue ?"))
		throw "Operation canceled"

	// delete output file
	outFile.delete();
}

mergedDoc = XLSUtil.createWorkbook(outFile)
mergedSh = XLSUtil.createNewPage(mergedDoc, "merged results")

files = FileUtil.getFiles(srcFolder, null, false, false, false)
mergedY = 0;

for(f = 0; f < files.length; f++)
{
	file = files[f]

	// ignore invalid entries
	if (!file.exists()) continue;

	println("Processing file: " + file.getName())

	doc = XLSUtil.loadWorkbookForRead(file)
	sh = doc.getSheet(0)

	// first file ?
	if (f == 0) XLSUtil.setCellString(mergedSh, 0, 0, "Image")
	
	for(y = 0; y < sh.getRows(); y++)
	{		
		// write image name in first column
		if (y > 0) XLSUtil.setCellString(mergedSh, 0, mergedY, file.getName())
		
		for(x = 0; x < sh.getColumns(); x++)
		{
			cell = sh.getCell(x, y)
			// not first row or first file
			if ((y > 0) || (f == 0)) XLSUtil.setCellString(mergedSh, x + 1, mergedY, cell.getContents())
		}

		// pass to next row
		if ((y > 0) || (f == 0)) mergedY++;
	}
}

XLSUtil.saveAndClose(mergedDoc)


Icy script publication Id : F2D4I9