
Json To Xlsx
Deprecated
Pricing
Pay per usage
Go to Store

Json To Xlsx
Deprecated
The act takes a JSON input and converts it to XLSX.
0.0 (0)
Pricing
Pay per usage
7
Total users
54
Monthly users
2
Last modified
3 years ago
Dockerfile
# This is a template for a Dockerfile used to run acts in Actor system.# The base image name below is set during the act build, based on user settings.# IMPORTANT: The base image must set a correct working directory, such as /usr/src/app or /home/userFROM apify/actor-node
# Second, copy just package.json and package-lock.json since it should be# the only file that affects "npm install" in the next step, to speed up the buildCOPY package*.json ./
# Install NPM packages, skip optional and development dependencies to# keep the image small. Avoid logging too much and print the dependency# tree for debuggingRUN npm --quiet set progress=false \ && npm install --only=prod --no-optional \ && echo "Installed NPM packages:" \ && (npm list --all || true) \ && echo "Node.js version:" \ && node --version \ && echo "NPM version:" \ && npm --version
# Copy source code to container# Do this in the last step, to have fast build if only the source code changedCOPY . ./
# NOTE: The CMD is already defined by the base image.# Uncomment this for local node inspector debugging:# CMD [ "node", "--inspect=0.0.0.0:9229", "main.js" ]
main.js
1const XLSX = require('xlsx');2const Apify = require('apify');3
4function toXlsxBuffer(array){5 //flattenRecords(array)6 const ws = XLSX.utils.json_to_sheet(array);7 const wb = {SheetNames:['results'], Sheets:{'results': ws}};8 const wopts = {bookType:'xlsx', bookSST:false, type:'binary'};9 const wbout = XLSX.write(wb, wopts);10 11 function s2ab(s){12 const buf = new ArrayBuffer(s.length);13 const view = new Uint8Array(buf);14 for(let i=0; i!=s.length; ++i){15 view[i] = s.charCodeAt(i) & 0xFF;16 }17 return Buffer.from(buf);18 }19 20 return s2ab(wbout);21}22
23function flattenRecord(record){24 if(record && typeof record === 'object'){25 for(const key in record){26 const value = record[key];27 if(value && Array.isArray(value)){28 if(value.length > 0){29 record[key] = undefined;30 //delete record[key];31 flattenRecords(value);32 for(let i = 0; i < value.length; i++){33 record[key + '/' + i] = value[i];34 }35 }36 }37 }38 }39}40
41function flattenRecords(data){42 for(let i = 0; i < data.length; i++){43 flattenRecord(data[i]);44 }45}46
47Apify.main(async () => {48 const input = await Apify.getValue('INPUT');49 let buffer;50 51 if(Array.isArray(input)){52 console.log(`Parsing a raw data array`)53 buffer = toXlsxBuffer(input);54 }55 else if(input.storeId && input.key){56 const jsonFromStore = await Apify.newClient().keyValueStore(input.storeId)57 .getRecord(input.key)58 .then((res) => res.value);59 buffer = toXlsxBuffer(jsonFromStore);60 }61 else{62 throw new Error('Invalid input');63 }64 65 const storeId = Apify.getEnv().defaultKeyValueStoreId;66 const url = "https://api.apifier.com/v2/key-value-stores/" + storeId + "/records/results.xlsx?rawBody=1&disableRedirect=1";67 const type = "application/octet-stream";68 await Apify.setValue('results.xlsx', buffer, {contentType: type});69 await Apify.setValue('OUTPUT', {output: url});70 console.log('Output URL: ' + url);71});
package.json
{ "name": "apify-project", "version": "0.0.1", "description": "", "author": "It's not you it's me", "license": "ISC", "dependencies": { "xlsx": "latest", "apify": "^2.2.2" }, "scripts": { "start": "node main.js" }}