0
votes

I need to write a script that allows me from to upload a pdf to a single cell in google sheets, and send an email to me with the pdf. As far as I know, there is no way to upload pdf or image files to google sheets, so I guess the only way to make this work is to have it upload to a google drive folder and then paste the link to the pdf in a cell, and email the link to me.

I've searched for quite some time now, but haven't been able to find any solutions to this.

Google has their own tutorial with sending a mail from sheets. I used this code for it:

var SENT = 'sent';

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; 
  var numRows = 100; 
  var dataRange = sheet.getRange(startRow, 1, numRows, 7);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[3]; 
    var message = row[4] + dataRange.getValue(); 
    var Sent = row[6]; 
    if (Sent != SENT) { 
      var subject = 'Invoice';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 6).setValue(SENT);
      SpreadsheetApp.flush();
    }
  }
}

This works fine, my problem is uploading the file to google drive and pasting the link in a cell.

I'm assuming the code will start with something like this:

function uploadFile() {
  var invoice = 'some local path to pdf file';
  var file = {
    title: 'Invoice',
    mimeType: 'application/pdf'
  };
  file = Drive.Files.insert(file, invoice);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

However I'm not sure which direction to go. Can anyone give me some tips?

1

1 Answers

0
votes

To upload a file to Google Drive by using Google Apps Script, consider to uses Google Picker. For details see https://developers.google.com/apps-script/guides/dialogs#file-open_dialogs