Posted 24 September 2024, 3:30 am EST
Hi,
How can I access the IFileInfo data of a FileUpload when valuePath is set to “name”?
Thanks
Forums Home / Spread / SpreadJS
Posted by: n.serrette on 24 September 2024, 3:30 am EST
Posted 24 September 2024, 3:30 am EST
Hi,
How can I access the IFileInfo data of a FileUpload when valuePath is set to “name”?
Thanks
Posted 25 September 2024, 1:03 am EST - Updated 25 September 2024, 1:09 am EST
Hi,
Based on my understanding, you would like to retrieve the IFileInfo object when using the fileUpload cell type, even when the valuePath is set to “name”.
When the valuePath is set to “name”, you can only access the file’s name from IFileInfo, as the valuePath acts as a key for retrieving specific properties of IFileInfo. Since the valuePath cannot be changed once the cell type is set, if you need access to the entire IFileInfo object, you should set the valuePath to undefined. Then, you can use the getValue method to retrieve the full IFileInfo object. Refer to the attached GIF “Steps.gif” and sample.
Gif:
Regards,
Priyam
Posted 25 September 2024, 3:39 am EST
Hi,
Thanks, for your reply, you have understood my need.
The issue I found with the solution you propose, is that I cannot use the value of the FileUpload in a formula,
ideally I would like the value returned by a reference on the FileUpload be the name of the file, but still be able to access the file itself. There should be a way to access it since it is possible to download the file when the value path is set to “name”.
Regards,
Posted 26 September 2024, 1:00 am EST
Hi,
Currently, the IFileInfo is stored internally in SpreadJS and, as far as I know, there isn’t a public API for direct access except for the above-mentioned way. However, you can retrieve the file information when valuePath is set to “name” by converting the sheet to JSON using the toJSON method. From there, you can get the file ID from the data table for the desired cell and retrieve the file from the attachments using this ID. Please refer to the attached snippet and sample for more details.
const fileUpload = new GC.Spread.Sheets.CellTypes.FileUpload();
fileUpload.valuePath("name");
sheet.setCellType(1, 1, fileUpload);
document.getElementById("getFileInfo").addEventListener("click", (e) => {
// Get sheet JSON
let json = spread.getActiveSheet().toJSON();
// Get file ID from dataTables for cell B2
let fileID = json.data.dataTable[1][1].fileGUID;
// Retrieve file info from attachments using the file ID
let fileInfo = json.attachments[fileID];
console.log(fileInfo);
});
In the sample, upload a file, click the button, and check the console for file info.
References:
toJSON: https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Sheets.Worksheet#tojson
Regards,
Priyam