Excel Import Scaffolding
Last modified by Clemens Robbenhaar on 2022/10/16 22:46
Bootstraps a import UI out of an excel attachment |
Type | Snippet |
Category | |
Developed by | Unknown |
Rating | |
License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Template that you can use to bootstrap an Excel import UI.
- First, it loops over attachments and shows a select list for the user to chose from which file to import.
- Then, it asks for which of the sheets of the Excel document to import from
- Then it executes the preview / import code inserted where specified in the snippet.
{{groovy}}
def book = xwiki.calc.getWorkbook(request.import_file)
if (book) {
def sheetName = request.import_sheet;
def sheet = book.getSheet(sheetName)
if (sheet) {
// your preview code here (show what would be going to be imported, etc.)
println """
{{html wiki=false clean=false}}
<form action="" method="post" class="xform">
<div style="max-width:300px">
<input type="hidden" name="xaction" value="confirmImport">
<input type="hidden" name="import_sheet" value="${request.import_sheet}">
<input type="hidden" name="import_file" id="import_file" value="${request.import_file}">
<p>
<span class="buttonwrapper">
<input class="button" type="submit" value="Import !" />
</span>
</p>
</div>
</form>
{{/html}}
"""
if (request.xaction && request.xaction == 'confirmImport') {
// your actual import code here
}
}
}
{{/groovy}}
def book = xwiki.calc.getWorkbook(request.import_file)
if (book) {
def sheetName = request.import_sheet;
def sheet = book.getSheet(sheetName)
if (sheet) {
// your preview code here (show what would be going to be imported, etc.)
println """
{{html wiki=false clean=false}}
<form action="" method="post" class="xform">
<div style="max-width:300px">
<input type="hidden" name="xaction" value="confirmImport">
<input type="hidden" name="import_sheet" value="${request.import_sheet}">
<input type="hidden" name="import_file" id="import_file" value="${request.import_file}">
<p>
<span class="buttonwrapper">
<input class="button" type="submit" value="Import !" />
</span>
</p>
</div>
</form>
{{/html}}
"""
if (request.xaction && request.xaction == 'confirmImport') {
// your actual import code here
}
}
}
{{/groovy}}
{{velocity}}
#if($doc.getAttachment("$!request.import_file"))
#set($fileName = $request.import_file)
#set($file = $xwiki.calc.getWorkbook($fileName))
#if("$!request.import_sheet" != '')
#set($sheet = $file.getSheet($request.import_sheet))
#end
#end
#if($file && $sheet)
[[Cancel>>$doc.fullName]]
#elseif($file && !$sheet) ## Select sheet
{{html wiki=false clean=false}}
<form action="" method="post" class="xform">
<div style="max-width:300px">
<input type="hidden" name="import_file" id="import_file" value="${fileName}">
<dl>
<dt>
<label for="import_sheet">Import sheet</label>
<span class="xHint">Sheet to be used for the import :</span>
</dt>
<dd>
<select id="import_sheet" name="import_sheet">
#foreach($sheetName in $file.getSheetNames())
<option value="${sheetName}">${sheetName}</option>
#end
</select>
</dd>
</dl>
<p>
<span class="buttonwrapper">
<input class="button" type="submit" value="Next" />
</span>
</p>
</div>
</form>
{{/html}}
#else
{{html wiki=true}}
<form action="" method="post" class="xform">
<div style="max-width:300px">
<input type="hidden" name="xaction" value="selectFile">
<dl>
<dt>
<label for="import_file">Import file</label>
<span class="xHint">File to be used for the import</span>
</dt>
<dd>
<select id="import_file" name="import_file">
#foreach($attach in $doc.attachmentList)
<option value=${attach.filename}>${attach.filename}</option>
#end
</select>
</dd>
</dl>
<p>
<span class="buttonwrapper">
<input class="button" type="submit" value="Next" />
</span>
</p>
</div>
</form>
{{/html}}
#end
{{/velocity}}
#if($doc.getAttachment("$!request.import_file"))
#set($fileName = $request.import_file)
#set($file = $xwiki.calc.getWorkbook($fileName))
#if("$!request.import_sheet" != '')
#set($sheet = $file.getSheet($request.import_sheet))
#end
#end
#if($file && $sheet)
[[Cancel>>$doc.fullName]]
#elseif($file && !$sheet) ## Select sheet
{{html wiki=false clean=false}}
<form action="" method="post" class="xform">
<div style="max-width:300px">
<input type="hidden" name="import_file" id="import_file" value="${fileName}">
<dl>
<dt>
<label for="import_sheet">Import sheet</label>
<span class="xHint">Sheet to be used for the import :</span>
</dt>
<dd>
<select id="import_sheet" name="import_sheet">
#foreach($sheetName in $file.getSheetNames())
<option value="${sheetName}">${sheetName}</option>
#end
</select>
</dd>
</dl>
<p>
<span class="buttonwrapper">
<input class="button" type="submit" value="Next" />
</span>
</p>
</div>
</form>
{{/html}}
#else
{{html wiki=true}}
<form action="" method="post" class="xform">
<div style="max-width:300px">
<input type="hidden" name="xaction" value="selectFile">
<dl>
<dt>
<label for="import_file">Import file</label>
<span class="xHint">File to be used for the import</span>
</dt>
<dd>
<select id="import_file" name="import_file">
#foreach($attach in $doc.attachmentList)
<option value=${attach.filename}>${attach.filename}</option>
#end
</select>
</dd>
</dl>
<p>
<span class="buttonwrapper">
<input class="button" type="submit" value="Next" />
</span>
</p>
</div>
</form>
{{/html}}
#end
{{/velocity}}
Prerequisites & Installation Instructions
This snippet assumes you've successfully installed the Excel Plugin on your wiki.