I am trying to save a set of time lapse to an Excel file in expo react native app which I am building. When I click save button, it throws this error
[Unhandled promise rejection: Error: Call to function ‘ExponentFileSystem.writeAsStringAsync’ has been rejected.]
I am using android device.
I look all over google, but I can’t find a solution for this issue. Does anyone have any idea how to solve this ?
Here is my code for the function that saves the file:
const writeArrayToExcel = async (dataArray) => {
const resultsMade = [["lap", "time"]];
dataArray.map((lapTime, index) => {
const lapName = `${lapWord} ${index + 1}`;
const valLap = `${displayTime(lapTime).hours}:${displayTime(lapTime).minutesSeconds}:${displayTime(lapTime).centiseconds}`;
resultsMade.push([lapName, valLap]);
})
let wb = XLSX.utils.book_new();
let ws = XLSX.utils.aoa_to_sheet(resultsMade);
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
const base64 = XLSX.write(wb, { bookType: 'xlsx', type: 'base64' });
const currentDate = new Date();
const formattedDate = `${currentDate.getFullYear()}-${(currentDate.getMonth() + 1).toString().padStart(2, '0')}-${currentDate.getDate().toString().padStart(2, '0')}_${currentDate.getHours().toString().padStart(2, '0')}-${currentDate.getMinutes().toString().padStart(2, '0')}-${currentDate.getSeconds().toString().padStart(2, '0')}`;
const filename = FileSystem.downloadDirectory + `Stopwatch-${formattedDate}.xlsx`;
FileSystem.writeAsStringAsync(filename, base64, {
encoding: FileSystem.EncodingType.Base64
}).then(() => {
//Sharing.shareAsync(filename);
});
};
const exportToExcel = () => {
writeArrayToExcel(results);
};
2
Answers
what solved my issue was adding a directory name to the filename
As suggested by Expo "writeAsStringAsync" content should be string.
If not, should be converted to string