How to access Google drive file with file name, without knowing the file ID? - google-apps-script

I have a script, which overwrites daily a Google Drive file (Google Spreadsheet) with new data. On overwriting, the file name remains the same, but the file ID changes.
I want to access the file with a Google App script scheduled daily with a time trigger to remove each daily portion of data into another file, so I'm able to make a time series report.
I know how to do this - but the method I know will work only if the file ID remains always the same - so I can't use this known method.
How can I access the file while knowing only its file name, which remains always the same, without knowing the the file ID?

I suggest you update the script that overwrites the file to just update the file. Then it's always the same file.
If that's not possible, you can search for the file using DriveApp.getFilesByName(). If you happen to know exactly which folder the file will be in, then you can narrow your query results by using Folder.getFilesByName().

Related

Is there a script function that would pull up my network files and let me pick one to import?

I'm setting up a file comparison in google sheets. One file type is an excel sheet sent to my email on the first of every month. The other file type is a .txt file autosaved onto my computer in a shared network from an app I have, and that one is saved every hour on the hour. I need the user to be able to click the button connected to the script, and give them the file explorer so they can pick the proper file. Any file they would pick is in the same folder, they just have to pick the one with the timestamp they want.
The excel sheet is already auto-uploaded into the drive and auto-uploaded into the file comparison. I can't figure out how to give the user the option to select the second file they want to upload (the one coming from my shared network) with the push of a button, other than having them manually click "file, import, import from my computer..."
Is there a way to do this?

How can I automatically import & replace data in a Google Sheet from a CSV that has been uploaded to our Google Drive?

We have some marketing software that pulls reports on email marketing and uploads a CSV automatically every week or month into our Google Drive. Our reporting software can't automatically pull data from a CSV, so I have to use Google Sheets.
How can I make it so that when a new CSV is uploaded onto the Drive (replacing the old CSV in the process), my Google Sheet will pull the new data and replace it over the old data?
The question isn't clear, meaning there are many "variables" to your question, and thus a variety of solutions which you can implement to solve the specific permutation you're looking for. But I'll try to answer your question with some considerations and suggestions:
"[A] new CSV is uploaded onto the Drive (replacing the old CSV in the process)." Are you sure this is happening? Every time you upload, say a hello.csv to Drive, it adds a new hello.csv file one rather than replacing an existing one with the same name. You would have to have an application that knows or can look up the original CSV Drive file ID and explicitly replace the file contents or upload a new version of that file.
If you do replace the original file via one of those two techniques, track the changes to that file via the Drive API, meaning your app can take action as soon as that file has been updated. See this page in the docs to learn how to detect changes.
The comment in the OP makes a suggestion from another SO Q&A, however that solution does not address the OP's question. Instead, it imports a CSV file to Google Drive as a Google Sheets (file) -- the CSV file never makes it to Drive as per the OP. Also, the OP is requesting something more than a mere import.
However, with that said, the OP's final request was: "[My] Google Sheet will pull the new data and replace it over the old data." There are 2 ways to do this:
a) The simplest/easiest way is to just overwrite all existing data in the Sheet with what's in the CSV file... old data, new data, it doesn't matter. Once the new CSV file has been imported and new Sheets file created, you can delete both the old CSV and corresponding Sheet. The (new) files can have the same names as their predecessors no problem. For that, build a solution similar to the SO Q&A mentioned above -- the CSV file will already be in Drive, so just create a new Sheets file and use the content from the CSV file on Drive (rather than from the local filesystem).
b) If you only want the deltas, you need to keep a copy of the old CSV and "diff" it with the new CSV, and only overwrite the rows that have changed, a much more complex solution, and one in which you'll have to use the Google Sheets API (because it's for spreadsheet operations while the Drive API is used for file operations).

I need a script to monitor a specific Google Drive folder whenever there is a change and notify me by email

I have been trying all day and I found this:
http://www.jellybend.com/2012/12/19/monitor-google-drive-folders-with-google-apps-script/
The attached script worked only partially for me. It doesn't respond to change to the subfolders even there are files inside the subfolders (eg. rename/delete the subfolder). It also seems to have errors if I delete and re-add the same file to the folder again, it just doesn't email me for the newly added "old file".
I also found this:
https://developers.google.com/drive/v2/reference/changes/list#examples
but unfortunately I am not really sure what those parameters are and I am just inexperienced in writing something like that.
Any help will be greatly appreciated! Thanks!
The file monitoring code at that link, is an Apps Script bound to a Sheet. An Apps Script can be bound to a Sheet, Doc, Form or Site. An Apps Script can also be a stand alone application. So, any code you may want to write, does not need to be in a spreadsheet.
An Apps Script can be set up to have a Time-driven event trigger.
There is also a Script Service to build Clock Triggers.
ClockTriggerBuilder Class
Using Time Driven Event Trigger, or a Clock Trigger you could use the getSize() method to return the amount of disk space used by the item:
Class - Folder - getSize Method
// This example logs the first file's size in bytes
// Note: This can also be used on a folder to get the size of its contents
var file = DocsList.getAllFiles[0];
Logger.log(file.getSize());
Of course, you would need to know what the original size of the folder or file was, and so you would need to store the current size somewhere. You could create a file for storing that information, or use the built in database that Apps Script has.
For storing your historical folder or file information you could use ScriptDB.
ScriptDB
Quote:
ScriptDB is a JavaScript object database for Google Apps Script. Each script project gets a database, which the script can use to save, update, and search JavaScript object data.
You could write historical file and folder info to a spreadsheet or document also.
Depending on who owns the file, and who is accessing the file, permissions would need to be granted, or you'd need to use oAuth2 to authenticate who has access to the file and folder information.
If you can't write all the code yourself, you could set up a shared Apps Script file, or find some other way to have people collaborate on the project.
I've been researching this on my own for some time and came up with a script bound to a spreadsheet as well. Hope it can help someone, as it seems that a lot of people are looking for something similar. My script is monitoring a folder and sends notification to all the current viewers of the folder as soon as there is new file added. Trigger to run the script could be set depending on the needs of the user. In my case hourly worked just fine.
Spread Sheet with the script: https://docs.google.com/spreadsheets/d/1CzVADjUTT2d9Y5OGDOnv37mCaO49kPt4RmnyZgzjTKA/edit#gid=0
If you would like to try this script just make sure you are logged in with your google account when you open the link and you should be able to make a copy of the spreadsheet.
This Google Script will send an email notification to you or other email addresses when a file in a Google Drive folder has been added, renamed, changed, or modified. http://baumbach.com/google-script-2/

Do Google Drive File IDs break?

I'm playing with the Google Drive API and one thing that I keep wondering about is IDs in the case of an arbitrary file (non-Google App) like an image.
If a user is working locally, offline and they turn off the Google Drive client. And then they make some changes to a file, let's say they rename it, move it from one folder to the next and edit it.
Will the ID remain the same when the client comes back on, or will it break? I imagine the client will interpret either one or all of the move, rename and edit as delete and new file.
Would appreciate any help here!
The file id (within Google Drive) remains the same from the original creation to its deletion.

Creating a Google Apps Script which Automatically Executes on Upload

I have an interesting fairly simple problem I am trying to solve but I am at a loss on the proper code to get this to working correctly.
Basically, I have an very old local application running which allow me to print a receipt of a transaction. I have the ability to enable this receipt to be printed to a specific file within a specific directory on my computer or I can set it up to print out automatically to a printer on my computer. Currently I print the data out to a file, then copy the contents of that file and paste it into a word document template and then manually go through and format the data so it looks clean (I should note that the data prints out in fixed field format requiring a specific font and size so its pasts correctly. After all this is done I then email the word or pdf document out.
Essentially what I am trying to do here is automate this method and I thought this should be possible using a Google Apps Script.
My thought here was that it should be possible to:
save all the original files from my application to a specific
directory on my computer
sync that folder with google drive
have a google apps script automatically execute upon upload
have the google apps script automatically use a specific document
template I create with a header/footer and then paste all the
contents from the new file which was uploaded
have the google apps script pattern match certain elements within the contents of
the document and properly format it.
save the document to a specific location with a specific file naming convention
automatically email the document as a doc or pdf based upon a
specific regex matched field within the source document.
Could anyone provide a specific code example which demonstrates how Google apps scripts can be utilized to monitor the contents of a specific Google drive folder to trigger an event immediately after a new file is uploaded?
Thanks in advance!