Recently doing a function is the cloud the export to excel form and copy of database information download link function, because didn’t do it before this function, so it is the old, baidu, through an hour of baidu, finally found a friend to share the example can be used, NieDaGe, thank you very much for his share,
We will divide it into the following steps:
2. Write cloud function code. 3. Export the table and copy download link
1. Install the Excel table export module
–save is not installed in package.json, so the node-xlsx module cannot be found by calling the function method
npm install node-xlsx --save
Copy the code
2. Write cloud function code
Open the index.js file, the following code to change is the cloud development environment, to change their own table header to change, content field to change
const cloud = require('wx-server-sdk') cloud.init({ env: }) const XLSX = require('node-xlsx') // Import Excel class library const db = cloud.database() // declare database object const _ = db.command exports.main = async (event, context) => {// main function entry console.log('event', event,context) try { let StuInfo = null if(event.openid) { StuInfo = await db.collection('devicesetList').where({ openId: Event.openid}).get() // Assign the obtained data object to the variable, } else {StuInfo = await db.collection('devicesetList').get()} let dataCVS = 'studentInfo-${math.floor (math.random ()*1000000000)}.xlsx' // Declare an Excel table whose name is generated using random numbers let allData = []; Let the row = [' device name ', 'IMEI number', 'the longitude, latitude, device address,' add person ', 'phone number', 'added time', 'the openId]; // The table header object alldata.push(row); For (let key = 0; for (let key = 0; key<StuInfo.data.length; key++) { let arr = []; arr.push(StuInfo.data[key].name); arr.push(StuInfo.data[key].imei); arr.push(StuInfo.data[key].lng ); arr.push(StuInfo.data[key].lat); arr.push(StuInfo.data[key].address); arr.push(StuInfo.data[key].nickName); arr.push(StuInfo.data[key].phone); arr.push(StuInfo.data[key].time); arr.push(StuInfo.data[key].openId); alldata.push(arr) } var buffer = await xlsx.build([{ name: "mySheetName", data: alldata }]); // Store the table in the repository and return the file ID return await cloud.uploadFile({cloudPath: dataCVS, fileContent: Buffer, // Excel binary})} Catch (error) {console.error(error)}}Copy the code
3. Export the table and copy the download link
Comment code directly open Excel table, the first requirement is to directly open, then become the download Excel requirements, so retained, ha ha!
ExportTap (e) {console.log('e', e) this.setData({exportSwidth: true }) let exportOpenId = this.data.exportOpenId console.log('exportOpenId', Wx.cloud. callFunction({name:"StuExcel", data:{openID :exportOpenId}, complete:res=>{ console.log('res callFunction', Res) wx.cloud.getTempFileurl ({// Obtain file download address (valid within 24 hours) fileList:[res.result.fileid], success:res=>{ util.hideLoading() console.log('getTempFileURL', Res) this.setData({tempFileURL: res.filelist [0]. TempFileURL, showUrl:true}) wx.setclipboardData ({// copy the link just obtained, After the success will automatically pop-up prompts replicated data: this. Data. TempFileURL, success: (res) = > {enclosing setData ({exportSwidth: False}) wx.getClipBoardData ({success (res) {wx.showmodal ({title: 'prompt ', content: "XLSX table address copied successfully, paste the address in the browser to download", success: Function (res) {if (res.confirm) {console.log(' confirm ')} else if (res.cancel) {console.log(' cancel ')}}}) console.log(res.data) // data } }) } }) // wx.downloadFile({ // url: this.data.tempFileURL, // success: res => { // util.hideLoading() // console.log('downloadFile res', res) // var filePath = res.tempFilePath // wx.openDocument({ // filePath: FileType: 'XLSX ', // showMenu: true,// Whether to display the upper-right menu // success: Res = > {/ / the console. The log (' open the document successfully, res) / /}, / / fail: err = > {/ / console log (' err, openDocument, err) / /} / /}); // }, // fail: err => { // util.hideLoading() // console.log('downloadFile err', err) // } // }) }, fail: err => { util.hideLoading() console.log('err getTempFileURL', err) } }) }, fail: err => { util.hideLoading() console.log('err callFunction', err) } }) }Copy the code
The last
I also want to thank those friends who share, my work brings convenience, so I will be willing to share my own things, also make it convenient for other friends, at the same time, sorting out the shared content is also to deepen this knowledge, if you feel helpful, remember to like and follow, thank you!!