Adding Replace Existing File to GmailToDrive App Script - Follow up Question - google-apps-script

This is an additional question off of the related post: Adding Replace Existing File to GmailToDrive App Script answered by #Tanaike.
I'm trying to use the code from the previous post with the Edit function with an xlsx file. Everything seems to be working fine where it grabs the file and saves it and it even updated the modified date in the google drive folder. However, the content of the excel file does not change from the original. I've enabled the Drive API. Is this compatible with xlsx file types? Is there something additional needed to actually update the content in the file and not just the metadata?
// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['xlsx'];
//Name of the folder in google drive i which files will be put
var folderName = 'YourfolderName;
//Name of the label which will be applied after processing the mail message
var labelName = 'Saved2';
function GmailToDrive(){
//build query to search emails
var query = '';
//filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
for(var i in fileTypesToExtract){
query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
}
query = 'in:inbox label:MyCustomLabel has:attachment ' + query;
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder;
if(threads.length > 0){
parentFolder = getFolder_(folderName);
}
var root = DriveApp.getRootFolder();
for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
//get attachments
var attachments = mesgs[j].getAttachments();
for(var k in attachments){
var attachment = attachments[k];
var isDefinedType = checkIfDefinedType_(attachment);
if(!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var existingFile = DriveApp.getFilesByName(attachment.getName());
if (existingFile.hasNext()) {
var file = existingFile.next();
Drive.Files.update({}, file.getId(), attachmentBlob);
} else { // Added
var file = DriveApp.createFile(attachmentBlob); // Added
parentFolder.addFile(file); // Added
root.removeFile(file); // Added
}
}
}
threads[i].addLabel(label);
}
}
//This function will get the parent folder in Google drive
function getFolder_(folderName){
var folder;
var fi = DriveApp.getFoldersByName(folderName);
if(fi.hasNext()){
folder = fi.next();
}
else{
folder = DriveApp.createFolder(folderName);
}
return folder;
}
//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
n = parseInt(8);
var date = new Date();
date.setDate(date.getDate() - n);
return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}
function getGmailLabel_(name){
var label = GmailApp.getUserLabelByName(name);
if(!label){
label = GmailApp.createLabel(name);
}
return label;
}
//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
var fileName = attachment.getName();
var temp = fileName.split('.');
var fileExtension = temp[temp.length-1].toLowerCase();
if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
else return false;
}

Because .xlsx is the Excel format, Apps Script can't modify its content because it's not a Google product, it's like trying to modify a pdf using Apps Script, but don't worry, you could convert the .xlsx file into the native Google Spreadsheet format and then handle it like a normal Google Spreadsheet. This function will do the conversion for you:
// this Function will convert your .xlsx files into native Google Spreadsheet format
function convertExceltoGoogleSpreadsheet(parentFolder) {
var files = parentFolder.getFiles();
while(files.hasNext()){
var file = files.next();
// If the file contains the .xlsx extention in its name
// then it will return an array with length 2
var filename = file.getName().split(/.xlsx?/);
if(filename.length > 1){
// build info to create the new file with Google Spreadsheet format
var resource = {
title: filename[0],
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{id: parentFolder.getId()}],
};
// Return the response after creating the file
return Drive.Files.insert(resource, file.getBlob());
}
}
}
Then, use that function in this part of your code:
...
var attachmentBlob = attachment.copyBlob();
var existingFile = DriveApp.getFilesByName(attachment.getName());
if (existingFile.hasNext()) {
var file = existingFile.next();
Drive.Files.update({}, file.getId(), attachmentBlob);
} else {
var file = DriveApp.createFile(attachmentBlob)
parentFolder.addFile(file);
root.removeFile(file);
}
// Let's change the format and insert a "Hello world" message
// ----START----
var spreadSheetResponse = convertExceltoGoogleSpreadsheet(parentFolder);
var spreadSheetId = spreadSheetResponse.getId()
var sheet = SpreadsheetApp.openById(spreadSheetId).getSheets()[0];
sheet.getRange(1, 1).setValue("Hello world");
// ----END----
...
Notice
In my code I left the .xlsx file in the Drive, if you want you could delete it after converting it iinto the native Google Spreadsheet format
Docs
This is what I used to help you:
SpreadsheetApp
Drive - Class File

Related

Get .xlsx attachment from Gmail and update rows in already existing Google sheet

I am new to Google AppScripts, and really struggling with achieving my end goal. The following code get's me very close, but I am wondering if anyone can please point me in a direction that enables me to do the following:
Extract .xlsx file from gmail that is received daily
Transfer the contents of the .xlsx file to an already existing Google sheet
Any advice would be hugely appreciated.
James
// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';
function GmailToDrive(){
//build query to search emails
var query = '';
//filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
for(var i in fileTypesToExtract){
query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
}
query = 'in:inbox has:nouserlabels ' + query;
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder;
if(threads.length > 0){
parentFolder = getFolder_(folderName);
}
var root = DriveApp.getRootFolder();
for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
//get attachments
var attachments = mesgs[j].getAttachments();
for(var k in attachments){
var attachment = attachments[k];
var isDefinedType = checkIfDefinedType_(attachment);
if(!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
parentFolder.addFile(file);
root.removeFile(file);
}
}
threads[i].addLabel(label);
}
}
//This function will get the parent folder in Google drive
function getFolder_(folderName){
var folder;
var fi = DriveApp.getFoldersByName(folderName);
if(fi.hasNext()){
folder = fi.next();
}
else{
folder = DriveApp.createFolder(folderName);
}
return folder;
}
//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
n = parseInt(n);
var date = new Date();
date.setDate(date.getDate() - n);
return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}
function getGmailLabel_(name){
var label = GmailApp.getUserLabelByName(name);
if(!label){
label = GmailApp.createLabel(name);
}
return label;
}
//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
var fileName = attachment.getName();
var temp = fileName.split('.');
var fileExtension = temp[temp.length-1].toLowerCase();
if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
else return false;
}
That's the codes I'm using for getting attachments from gmail to existing spreadsheet.
By setting up time driven trigger.
In order to make this function run faster. set the label name and the thread limites
function attachfromGmail(attachmentname, destsheet_id, destsheetname, label_name, all_or_part) {
const folder = DriveApp.getFolderById("xasdawfes2342ke");
const labels = GmailApp.getUserLabelByName(label_name).getThreads(0, 10);
const threads = GmailApp.getMessagesForThreads(labels);
const today_date = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yyyy");
var fileid_FromGmail = "";
threads.forEach(threads => {
threads.filter(messge => {
if (Utilities.formatDate(messge.getDate(), "GMT-6", "MM/dd/yyyy") == today_date) {
messge.getAttachments().filter(attachments => {
if (attachments.getName() == attachmentname && messge.isUnread() === true) {
messge.markRead();
const blob = attachments.copyBlob();
var file_FromGmail = Drive.Files.insert(
{ title: attachmentname, parents: [{ "id": folder.getId() }] },
blob,
{ convert: true }
);
fileid_FromGmail = file_FromGmail.id;
}
})
}
})
})
const created_file = SpreadsheetApp.openById(fileid_FromGmail);
const values = created_file.getDataRange().getDisplayValues();
const destfiles_sheet = SpreadsheetApp.openById(destsheet_id).getSheetByName(destsheetname);
if (all_or_part === 'all' && values != undefined) {
destfiles_sheet.clear();
destfiles_sheet.getRange(1, 1, created_file.getLastRow(), created_file.getLastColumn()).setValues(values);
} else if (values.length === 1 && values.toString() != 'No Data Available' || values.length > 1) {
values.shift()
destfiles_sheet.getRange(destfiles_sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
//delete created file
DriveApp.getFileById(fileid_FromGmail).setTrashed(true);
}
It works well, but I'm trying to get a better way to do the job. Instead of insert a new file and delete. I know we can use csv parse function to get the data. but I can not fix the leading 0 issue in the csv file.
Then I'm trying to use XML file to transfer data, still working on it.
I will update this post if I get a better solution.

Copying attachments from Gmail to Google Drive folder and overwriting old files with Apps Script

I am trying to user Google Apps Script to automate downloading attachments from a Gmail inbox to a specific folder on the account's Google Drive. I found the code below that does that part well, but I also need it to overwrite files of the same name with the new file or delete the old file and upload the new in its place. Also, it still seems to be downloading all filetypes from emails that have more than one attachment when I just need .xlsx files to be copied. Any help would be much appreciated. (credit goes to googleappsscript.org for the code)
// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = '#indrive';
function GmailToDrive(){
//build query to search emails
var query = '';
//filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+getDateNDaysBack_(1)+
for(var i in fileTypesToExtract){
query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
}
query = 'in:inbox has:nouserlabels ' + query;
// query += ' after:'+getDateNDaysBack_(1);
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder;
if(threads.length > 0){
parentFolder = getFolder_(folderName);
}
var root = DriveApp.getRootFolder();
for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
//get attachments
var attachments = mesgs[j].getAttachments();
for(var k in attachments){
var attachment = attachments[k];
// var isDefinedType = checkIfDefinedType_(attachment);
// if(!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
parentFolder.addFile(file);
root.removeFile(file);
}
}
threads[i].addLabel(label);
}
}
//This function will get the parent folder in Google drive
function getFolder_(folderName){
var folder;
var fi = DriveApp.getFoldersByName(folderName);
if(fi.hasNext()){
folder = fi.next();
}
else{
folder = DriveApp.createFolder(folderName);
}
return folder;
}
//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
n = parseInt(n);
var date = new Date();
date.setDate(date.getDate() - n);
return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}
function getGmailLabel_(name){
var label = GmailApp.getUserLabelByName(name);
if(!label){
label = GmailApp.createLabel(name);
}
return label;
}
//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
var fileName = attachment.getName();
var temp = fileName.split('.');
var fileExtension = temp[temp.length-1].toLowerCase();
if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
else return false;
}
Here is a sample code:
// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['xlsx'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = '#indrive';
function GmailToDrive() {
var query = '';
for(var i in fileTypesToExtract){
query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
}
query = 'in:inbox has:nouserlabels ' + query;
Logger.log(query);
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder = getFolder_(folderName);
for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
//get attachments
var attachments = mesgs[j].getAttachments();
for(var k in attachments){
var attachment = attachments[k];
Logger.log(attachment.getName());
Logger.log(attachment.getContentType())
//Check if attached file is xlsx file
if(attachment.getContentType() == MimeType.MICROSOFT_EXCEL){
var attachmentBlob = attachment.copyBlob();
//Check if filename exist in the drive folder then remove the file
var files = parentFolder.getFilesByName(attachment.getName());
while (files.hasNext()) {
var file = files.next();
Logger.log("FILE EXIST. Removing.....");
//Remove existing file
file.setTrashed(true);
}
//Create a new file in the drive folder
var newFile = parentFolder.createFile(attachmentBlob);
}
}
}
threads[i].addLabel(label);
}
}
function getGmailLabel_(name){
var label = GmailApp.getUserLabelByName(name);
if(!label){
label = GmailApp.createLabel(name);
}
return label;
}
//This function will get the parent folder in Google drive
function getFolder_(folderName){
var folder;
var fi = DriveApp.getFoldersByName(folderName);
if(fi.hasNext()){
folder = fi.next();
}
else{
folder = DriveApp.createFolder(folderName);
}
return folder;
}
Modifications done:
Replace fileTypesToExtract to just extract .xlsx files
Check if current attachment is a xlsx file using GmailAttachment.getContentType()
Include checking if the current attachment exist in the drive folder using Folder.getFilesByName(name). Then remove the file using File.setTrashed(trashed)
Create a new file using Folder.createFile(blob)
In your script you commented out the inDefinedType functionality.
with regard to replacing file content or deleting files of same name you could
look for all of the files in parentFolder with getFilesByName() and trash all of the old ones.

Script for uploading web files (via links in Google Spreadsheet) to Google Drive

I'm trying to write script for Google Spreadsheet which will upload all files linked in column A (only URLs) to specific folder (which name is written in column B).
table example
I'm partially succesfull. I can upload first linked file. But I don't know how to edit this script to upload all the files. Where am I wrong?
This script was found here and edited by me.
function downloadFile() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Pokus");
var fileURLs = sheet.getRange(1, 1, sheet.getLastRow()).getValues();
var foldernames = sheet.getRange(1, 2, sheet.getLastRow()).getValues();
for (i = 0; i < fileURLs.length; i++) {
var response = UrlFetchApp.fetch(fileURLs[i], {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob();
var folder = DriveApp.getFoldersByName(foldernames[i]).next();
if (folder != null) {
var file = folder.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
}
var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
return fileInfo;
}
}
I'm beginner in writing scripts and I'm not experienced. Could someone help?
How about this modification?
Modification points :
There is return fileInfo; in for loop. So at 1st loop, it's returned from the function. By this, only first linked file can be uploaded.
All information can be retrieved by modifying fileInfo to an array.
Modified script :
function downloadFile() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Pokus");
var fileURLs = sheet.getRange(1, 1, sheet.getLastRow()).getValues();
var foldernames = sheet.getRange(1, 2, sheet.getLastRow()).getValues();
var fileInfo = []; // Added
for (i = 0; i < fileURLs.length; i++) {
var response = UrlFetchApp.fetch(fileURLs[i], {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob();
var folder = DriveApp.getFoldersByName(foldernames[i]).next();
if (folder != null) {
var file = folder.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
}
fileInfo.push({ "rc":rc, "fileName":fileName, "fileSize":fileSize }); // Modified
}
return fileInfo; // Modified
}
Note :
When there are many upload files, an error might occur.
If I misunderstand your question, I'm sorry.

How to write google script to automatically call data from a google folder drive that contains a CSV file into one Google Sheets

I am trying to write a google script that will allow me to go into my google drive folder called "MeadJohsnon" and pull 'Temperature Calibration.csv' to google sheets. I have never used google script before. Currently I have the "Save Email and Attachment" Add-ons. This add-on is pulling .cvs files that my team is sending me from the field. They use "TapForms" and then send the form via email to my gmail. So, I got the email sending that attachment to my google drive but I need help with the script, so Drive will automatically get those .cvs files and put the information into one google sheet. I say ONE google sheet because although I have a team sending in forms, all the forms have the same information on them.
This is what I have done so far. The fourth line gives me a
function loadingCSV() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sht=ss.getActiveSheet();
sht.clearContents();
var data = loadFile();
var dataA =Utilities.parseCsv(data);
var rng = sht.getRange(1, 1, dataA.length, dataA[0].length);
rng.setValues(dataA);
}
I would just like feedback on how to fix my error or what I could do instead. As stated this is my first time using google script, my specialty is ASP.net lol not script. Thank you.
function loadingCSV() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sht=ss.getActiveSheet();
sht.clearContents();
var data = loadFile();
var dataA =Utilities.parseCsv(data);
var rng = sht.getRange(1, 1, dataA.length, dataA[0].length);
rng.setValues(dataA);
}
function loadFile(filename,folderID)
{
var filename = (typeof(filename) !== 'undefined')? filename : 'Temperature Calibration.csv';
var folderID = (typeof(folderID) !== 'undefined')? folderID : '0B8m9xkDP_TJxUUlueHhXOWJMbjg';
var fldr = DriveApp.getFolderById(folderID);
var file = fldr.getFilesByName(filename);
var s = '';
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
s = fi.getBlob().getDataAsString();
}
}
return s;
}
Okay this will append the files to the active spreadsheet you'll probably have to open the spreadsheet by id and use getSheetByName to get the sheet you want because this spreadsheet probably won't be active all the time when the trigger is running. I assume the the files all end in .csv. I rename them to .old after reading the data so that the program won't read them multiple times.
function appendingCSV() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sht=ss.getActiveSheet();
var drng = sht.getDataRange();
var lastRow = drng.getLastRow();
var data = loadFiles();
var dataA =Utilities.parseCsv(data);
if(dataA.length>0)
{
var rng = sht.getRange(lastRow + 1, 1, dataA.length, dataA[0].length);
rng.setValues(dataA);
}
else
{
SpreadsheetApp.getUi().alert('No Data Returned from LoadFiles');
}
}
function loadFiles(folderID)
{
var folderID = (typeof(folderID) !== 'undefined')? folderID : '0B8m9xkDP_TJxUUlueHhXOWJMbjg';
var fldr = DriveApp.getFolderById(folderID);
var files = fldr.getFiles();
var s='';
var re = /^.*\.csv$/i;
while (files.hasNext())
{
var file = files.next();
var filename = file.getName();
if(filename.match(re))
{
s += file.getBlob().getDataAsString() + '\n';
file.setName(filename.slice(0,-3) + 'old');
}
}
return s;
}

How to permanently delete Mails within scripts via MailApp?

I have found the following script in this forum and works really smoothly (takes files, saves files in the right folder, etc.). Namely I receive a txt. file every morning with relative quantities which I would like to import and save to my Gdrive (to later use in other scripts). The file is named with todays date which I have used the Utilities.formatDate function to obtain.
Unfortunately every time I run the script I get all files from even from previous days which would spam my folder full in a couple of days. Therefore my question is whether one can delete the emails after the script that my google search query will not find the previous emails the following day?
Any help with this conundrum appreciated!!
P.S: Email comes from the same recipient, with an identical subject "Stock_Load", however the attachment is named "date"_Todays Date.
// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['txt'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';
function GmailToDrive(){
//build query to search emails
var query = 'filename:stock_'+Utilities.formatDate(new Date(), "GMT", "yyyyMMdd").toString()+'.gpg.txt';
//filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
for(var i in fileTypesToExtract){
query += (query == '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
}
query = 'in:inbox has:nouserlabels is:unread' + query;
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var Folder = DriveApp.getFolderById("XXX");
for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
//get attachments
var attachments = mesgs[j].getAttachments();
for(var k in attachments){
var attachment = attachments[k];
var isImageType = checkIfImage_(attachment);
if(!isImageType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
Folder.addFile(file);
}
}
threads[i].addLabel(label);
}
};
//This function will get the parent folder in Google drive
function getFolder_(folderName){
var folder;
try {folder = DriveApp.getFolder(folderName)}
catch(e){ folder = DriveApp.createFolder(folderName);}
return folder;
}
//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
n = parseInt(n);
var today = new Date();
var dateNDaysBack = new Date(today.valueOf() - n*24*60*60*1000);
return dateNDaysBack;
}
function getGmailLabel_(name){
var label = GmailApp.getUserLabelByName(name);
if(label == null){
label = GmailApp.createLabel(name);
}
return label;
}
//this function will check for filextension type.
// and return boolean
function checkIfImage_(attachment){
var fileName = attachment.getName();
var temp = fileName.split('.');
var fileExtension = temp[temp.length-1].toLowerCase();
if(fileTypesToExtract.indexOf(fileExtension) != -1) return true;
else return false;
}