Need a Script to pull specific columns from an excel report that I receive to Gmail multiple times a day

424 views
Skip to first unread message

Rezwana Islam

unread,
Feb 15, 2022, 2:53:18 PM2/15/22
to Google Apps Script Community
Multiple times I day I receive automated reports of HR data to my Gmail from Oracle Fusion. I do not have access to Oracle Fusion. I would like to automatically pull a few columns containing relevant data to compile a .csv report. How can I do that?

Enrique Barrueto

unread,
Feb 15, 2022, 6:49:50 PM2/15/22
to google-apps-sc...@googlegroups.com
Show me an example to see what kind of data you have...

El mar, 15 feb 2022 a la(s) 14:53, Rezwana Islam (rezwan...@hbc.com) escribió:
Multiple times I day I receive automated reports of HR data to my Gmail from Oracle Fusion. I do not have access to Oracle Fusion. I would like to automatically pull a few columns containing relevant data to compile a .csv report. How can I do that?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/db383ac2-10fa-404c-a084-83665c06c0aan%40googlegroups.com.

Rezwana Islam

unread,
Feb 16, 2022, 10:48:29 AM2/16/22
to Google Apps Script Community
This is a sample of the type of data:

Untitled.png

Edward Ulle

unread,
Feb 16, 2022, 4:09:35 PM2/16/22
to Google Apps Script Community
Is it an attachment or embedded in the email?  And if an attachment what type?

Rezwana Islam

unread,
Feb 16, 2022, 4:30:06 PM2/16/22
to Google Apps Script Community
It is attached as an .xlsx file. 
I only need the data in the 'person number' and 'name' columns from the file. 

Edward Ulle

unread,
Feb 16, 2022, 4:42:05 PM2/16/22
to Google Apps Script Community

Martin Molloy

unread,
Feb 16, 2022, 5:28:31 PM2/16/22
to google-apps-sc...@googlegroups.com
You can use the add on "Save Emails and Attachments" to automate the process of taking attachments from an email and saving the attachment to your Google Drive. You can set it up to run automatically on receipt of the email.
It's created by Digital Inspiration and costs $99/year. It's free to try. I have no connection with the developers, I just use it and find it very useful.

You can then use my add-on SheetsIE to automate the process of importing those saved files into a google sheet and outputting a sheet or CSV file. You can set it up to run every hour. 
It costs $144 a year. This is the link if you want to install it - https://workspace.google.com/marketplace/app/sheetsie/369803151024
Again it's free to try.

Let me know if you have any questions.

Martin 

Edward Ulle

unread,
Feb 16, 2022, 5:52:52 PM2/16/22
to Google Apps Script Community
I can't guarantee this will work.  It works for my test case but I don't have an Excel file that matches your specifics.

function onOpen() {
  var menu = SpreadsheetApp.getUi().createMenu("File Picker");
  menu.addItem("Get XLSX","getXLSX");
  menu.addToUi();
}

function getXLSX() {
  try {
    var threads = GmailApp.getInboxThreads();
    // you will need to change the subject "XLSX Test" to your subject
    var thread = threads.find( email => email.getFirstMessageSubject() == "XLSX Test" );
    var messages = thread.getMessages();
    // assuming the attachement is attached to message 1
    var attachments = messages[0].getAttachments();
    var xlsxBlob = attachments[0]; // Is supposes that attachments[0] is the blob of xlsx file.
    var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlsxBlob).id;
    var sheet = SpreadsheetApp.openById(convertedSpreadsheetId).getSheets()[0]; // There is the data in 1st tab.
    var data = sheet.getDataRange().getValues();
    Drive.Files.remove(convertedSpreadsheetId); // Remove the converted file.
    var spread = SpreadsheetApp.getActiveSpreadsheet();
    // you will need to change Sheet2 to your sheet name
    var sheet = spread.getSheetByName("Sheet2");
    // I can't really test this part because I don't use Excel and have no test file with more than 1 column
    function extractColumns(row) {
      row = row.slice(3);   // remove columns D and up
      row = row.slice(1,1); // remove column B
      return row;
    }
    data.forEach( extractColumns );
    console.log(data);
    sheet.getRange(1,1,data.length,data[0].length).setValues(data);
  }
  catch(err) {
    console.log(err);
  }
}


Edward Ulle

unread,
Feb 17, 2022, 7:00:21 AM2/17/22
to Google Apps Script Community
Oops the part I couldn't test, well I tested it with a simple case.  Please replace the with the following.

    function extractColumns(row) {
      row = row.slice(0,3);   // remove columns D and up
      row.splice(1,1); // remove column B
      return row;
    }
    data.map( extractColumns );

Ed Sambuco

unread,
Feb 17, 2022, 3:05:15 PM2/17/22
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages