exports.writeFireToExcel = functions.https.onCall(async(data, context) => {
// Create a new instance of a Workbook class
const workbook = new xl.Workbook();
// Add Worksheets to the workbook
const worksheet = workbook.addWorksheet('Visa Work List');
const ref = firebaseDb.ref('path');
//firebase functions that return stuff must be done in a transactional way
//start by getting snap
return await ref.once('value').then(snapshot =>{
var style = workbook.createStyle({
font: {
bold : true,
},
});
//write workbook
worksheet.cell(1, 1).string('input').style(style);
//....write the rest of your excel
return
//
}).then(function (){
console.log('workbook filled');
//second part of transation - write the excel file to the temp storage in firebase
//workbook.write doesnt return a promise so ive turned it into a promise function
return new Promise((resolve,reject) =>{
workbook.write(tempFilePath, function (err, stats) {
if (err) {
console.error(err);
reject(err)
}else{
resolve()
}
});
})
}).then(function(){
console.log("File written to: " + tempFilePath);
//read the file and check it exists
return new Promise((resolve,reject) =>{
fs.readFile(tempFilePath, function (err, data) {
if (err) {
reject(err)
}else{
resolve()
}
})
})
}).then(function(){
console.log("writing to bucket");
//write the file to path in firebase storage
var fileName = 'VisaSummaryList.xlsx';
var folderPath = uid + "/excelFile/";
var filePathString = folderPath + fileName;
return bucket.upload(tempFilePath,
{ destination: filePathString}).then(function(){
return filePathString;
})
}).catch(err => {
throw err;
});
});
该函数返回FireBase存储中的文件路径。在您的Android应用程序中,只需:
//firebase storage reference, result being whats returned from the firebase function
val fbstore = FirebaseStorage.getInstance().reference.child(result)
fbstore.getFile(myFile)
3条答案
按热度按时间0kjbasz61#
首先,你必须从firebase获取所有数据。
Read Data From Firebase database
然后你必须从数据生成csv文件。
How to create a .csv on android
之后,您可以发送csv文件从其路径作为附件的邮件
How to send an email with a file attachment in Android
kkih6yb82#
首先在firebase项目中安装excel4node包,然后将其导入index.js
也导入这些文件处理
这就是你返回函数的样子
该函数返回FireBase存储中的文件路径。在您的Android应用程序中,只需:
afdcj2ne3#
将此添加到您的构建中。gradle
然后,创建一个数据类,如下所示:
然后,添加一个函数,类似于以下内容:
这个例子来自我的项目,它100%有效。请根据自己的需要自由填写修改。