Actor picture

Execution ID To XLS Send

vaclavrut/executionid-to-xls-and-send

This actor takes JSON at input, creates it as XLS from the execution ID result and then sends a link by email. The URL is also outputted in the log. Solves attaching XLS files to emails.

No credit card required

Author's avatarVaclav Rut
  • Modified
  • Users3
  • Runs415
Actor picture

Execution ID To XLS Send

Based on the apify/actor-node-basic:v0.21.10 Docker image (see docs).

const XLSX = require('xlsx');
const Apify = require('apify');
const _ = require('underscore');
const Promise = require('bluebird');
const request = require('request-promise');

Apify.main(async () => {
    Apify.setPromisesDependency(Promise);
    console.log("Start");
    const attachments = [];
    
    // get Act input and validate it
    let input = await Apify.getValue('INPUT');
    let finishWebhookData;
    console.log(JSON.stringify(input));
    if(input.data){
        //run from webhook
        finishWebhookData = JSON.parse(input.data);
        finishWebhookData._id = input._id
    }else if(input){
        //manual run from console
        finishWebhookData = input;
    }else{
         throw new Error('Input is missing!');
    }

    console.log("ID is -> "+ finishWebhookData._id)
    console.log("Create XLS from json.");
    
    const run = await Apify.call(
    	'petr_cermak/execution-to-xlsx',
    	{"_id": finishWebhookData._id});
    
   
    const xlsUrl = run.output.body.output;
    console.log(JSON.stringify(run));
    
    const xlsSize = parseFloat(run.output.size);
    console.log("We have XLS url: "+xlsUrl);
    console.log("Size of XLS is: "+xlsSize);
    const runMail = await Apify.call(
    	'apify/send-mail',
        {to: finishWebhookData.reciever,
        subject: finishWebhookData.subjectText,
        text: "Hello, here is the link for XLS result: "+ xlsUrl,
        attachments: attachments
        },
        {});
    
    
    /*
    //Next step to attach the file if the size is ok
    if(xlsSize < 10000000){
      console.log("Creating attachement, size is ok.")
      var attachement = await request({ url: xlsUrl, encoding: null });
      attachments.push({
            filename: `data.xls`,
            data: attachement,
        });
      }else{
        console.log("We cant send it as attachement, file is too big.")    
      }
      */
      /*
    console.log("Now we can send the mail.");
    await Apify.setValue('OUTPUT', {output: xlsUrl});
    const runMail = await Apify.call({
        actId: 'apify/send-mail',
        input: {
            contentType: 'application/json; charset=utf-8',
            body: JSON.stringify({
                to: finishWebhookData.reciever,
                subject: finishWebhookData.subjectText,
                text: "Hello, here is the link for XLS result: "+ xlsUrl,
                attachments: attachments
            })
          
        }
    });
    console.log("Mail sent, done.")
    */
});