Replace or Update Files in Google Drive with Apps Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back in this tutorial we are going to replace or update a text file that's been saved in Google Drive with some Google appscript to play along and encourage that you do you can grab a copy of this sta script in the link in the description below once you've made a copy we get cracken okay you're back so as you can see this is a standalone Google appscript file and in this stand loone file we have two functions we've already generated these are going to be our run functions first one is called runes number one or runes and then runes 2 this will be used for our testing uh in runes we have a have a text variable a file name variable and a folder ID and the same for our runes number two so what we're going to do is create a new function which is sort of emulates a class here that we going to use to call on to either update or replace our Target file which is this song that never ends text file here so let's get cracken so we're going to do things a little bit differently to a normal function that you might be used to there kind of a gateway to learning about classes but this is sort of a a quasy class here if you're at that stage in your learning cycle this will be good for you so let's go ahead and create a function and we'll call this file right we'll use the first letter Capital Syntax for classes but again not quite a class this one okay so what's going to happen in here it's going to return uh a object containing two properties and those properties are going to be functions themselves we'll contain those properties in a public API okay and we will create that public API up here so this will be uh const public API is equal to a object here okay so it's empty object so why can I edit a constant variable because the constant variable is only uh referring to what this is this outer object here so we can still make edits inside that object okay so let's do some js doc to help with our do a here yeah I know the fun stuff so what does the what does the function do it updates or it replaces the existing file data and it takes it it's going to have three parameters right it's going to be that text the file name and the folder so we want pam pam not pram and the first one and they're all going to be strings and this first one is going to be the folder ID uh we don't need any more explanation than that I think it's fairly clearly stated so that two more times and just change these last values here the next one will be the title and the last one is going to be the text and let's add these into this parameter space here so folder ID title text and what is it going to return well that's going to return two parameters uh two properties of an object right so it will return something special let's make a type definition for this that we'll call file right and then add to that in the moment and we'll explain what it is it will uh return just this object replace or uh update cool okay so up the top here let's make this file right type definition so we can see it when we're working okay first thing we'll say at type def it's going to be equal to file right and then the first property is going to be a function uh that's going to be called replace and the next one is going to be a function it's going to be called update cool nice okay so the first thing we need to do is to get the folder ID so the containing folder ID that we'll have so we haven't got it here so we need to get this folder ID first so let's go over to our file here this will be our folder idea we can see that's going to be in this test folder so I'm going to hit contrl C to bring this over and put that down here and comment it out I'm just going to grab this little folder ID here contrl C and lock that in there and we may as well back it in runes too for our next our test later as well cool now that we have our folder ID we can go up here and we'll say const folder is going to be equal to drive app.get folder by ID and that will be the folder ID oh const and then next we need to find the file to see if it exists right so if it hasn't already been generated just like now it's not there then we need to uh check to see if it's there or not which we can do fairly simply so let's create a constant variable called find file and that's going to be equal to boulder. getet files by name because we know in Google Drive we can have multiple files with the same name and then we want to put in the title here so dle now note in our title you'll also need the extension of the file so I've got the txt here you have might have Js or HTML Etc now we need to check if the file actually exists once we've got that let's say const file is equal to and then we'll say if find file exist so find file has next so if there's an item in the find uh the get files by name array that it generates so if it has next put a little question mark in here and we will say find file do next so we just want the next one uh we'll just assume that we only want one file the same name otherwise we want to grab the folder and then we'll say create file and here we can say we can add in the title and then we'll add in a empty string to create the text file cool so if it doesn't exist we'll create the text file ready for us to go okay so now we can work on our little public API so let's actually move this public API variable down here to keep it all together and we're going to create our two public apis so we'll say Public public API DOT first one is going to be replace and we'll create a function out that one and and this will be a nameless one for replace all we need to do is to reset the content of the file that we have here to the text that we're drawing in so we can say here file do set content here and type in text cool otherwise if we want to have if we want to update it so a pen to the end of this file we can use this update function that we're going to generate and that's going function and here we first need to get the content so we will say const content is going to be equal to file. getet blob so it's going to get us some blob data and then we want that blob data as a string so we can use the get content get data as string cool now that we have the content we can then first add in our content then the text so now we can say file do set content just like we did before and we first want to add in the content and we'll probably want to put things on the next line actually let's uh let's use template literals here to keep things nice and ordered and we'll put a dollar sign curly braces in here and the first thing first variable we need to add is in our content and then we want a new line back sln and then we want to add in the text so pin the text to the end and then we should be done so basically we need to on our file right we're going to add in these parameters here we're going to look for the F we're going to grab the folder that the file should be in and then see if the file exists if the file does exist then we need to if there is a file of that name we want to get the next one assuming there's only one file of that name in it otherwise we want to create that file once that occurs we're going to uh grab the the replace function here or the update function and replace will set the content so it's going to uh just create new content for that particular file and update is going to draw all the content from that file as a string and then add that in first to the set content and then add append the text to the end of it okay let's try out our runes so let's let's go file right and remember this is a function that we're going to add in some details the first one is going to be the folder ID which is uh folder ID here then we want the title which is going to be the file name and then we're going to add in the text Okay so we've got everything there and then what do we want to do to the end of it we want to let's say replace this time and that's a function don't forget okay let's it safe and let's go ahead and click on runes now the first time you have to run this we have to go through authorization after that you'll be fine okay so you can see over the right hand side that our text file has been generated let's give it a d double click to see what's inside and then we've got our little song in here now if we replace it it's going to using runes number one it's just going to replace it with the same song here it's going to be a bit weird to see so let's just use this runsy number two here with contrl c contrl v down here make sure it's all lined up Tab and let's just go into runes two and hit run okay and double click to see what's inside that was crazy that was crazy cool all right so let's go ahead and instead let's first replace the current text of that was crazy to back to our song in Run's one and then instead of replace we will update so once it's replaced it's going to run this one time so it should produce this two times now cuz we got to replace and then an update so let's go back to Run's one and hit run close this so it won't refresh yet okay let's double click open it up and there you go we've got the song occurring two times let's hide this one and hit runes again close that while we're waiting and we can see the file has been updated for a third rendition of The Song That Never Ends just for fun let's do it the fourth time because it does indeed go on and on my friends double click there you have it four more times wonderful okay everything's running successfully so in this tutorial we've managed to create a new file if it doesn't exist in a Target folder and then add some text to it or update some text by pending it to the end let me know how you're going to use this in your own project I always find it interest interesting to see and it's great inspiration for other developers like yourself if you've enjoyed this tutorial please hit that like button and if you want to see more tutorials like this subscribe until next time
Info
Channel: Yagisanatode -Scott-
Views: 217
Rating: undefined out of 5
Keywords:
Id: mi6Wb-da-GQ
Channel Id: undefined
Length: 13min 41sec (821 seconds)
Published: Wed Apr 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.