Apps Script Create a Doc How to create a Google Doc using Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
go over to your google drive log into your google account and then go over to drive.google.com and forward slash drive so this is where your drive is located we're going to create a brand new script so under the top at the top left side under more you're going to have an option for apps script so go ahead and create and this is going to be just creating that brand new script and this is going to be give it a name doc test and that's the name of the application that we're going to be creating and for this by default it's always going to start out the editor is going to start out with a function called my function so let's uh rename this and i'll call it my doc maker and within this function we want to set up and create our doc so this can be done by connecting to the document app and the document app service gives us an opportunity to create docs so it just requires the one parameter and that's going to be the document name let's run the code so within the drive right now i only have the apps script here so i don't have any docs within the root directory so running the script we're going to be first asked for permissions in order to create that document so select the review permissions select the account that you want to run the google apps script with and then under advanced you can go to the application it's going to say that it's unsafe i because this hasn't been submitted to google it'll give you details of what account which one of your google accounts is accessing the app and running the app with that google account and it'll give you the permissions that you're providing to the application to run if you do want to remove any permissions at any time you can click the google account and that will bring you to the myaccount.google.com forward slash permissions so let's go ahead and allow to create that file and now when i go back to the drive there's our brand new file that we've created we can select that file and we can add to that file so let's select that file and within the logger log now that we're creating the doc we can get the id of the doc and the id can be used in order to reference the doc content so this time when we run the doc maker we're going to output an id into the log so let's take that id and create a brand new function and this time we're going to update the doc so create a function to update the doc create a variable using that id that we just generated this will allow us to access the document so now we've got the doc there with the doc name and there's nothing in that doc but with google apps script we can actually select the element and the content within the dock and that's within the body object so first we need to get the doc object and because this is already a created one we can open it by id and this is where we needed the id value for opening the dock the id if you need to get an id of any one of your docs the easiest way is to open it from your drive and select within the url so that's going to give you the id of that current doc so that you can always reference it as needed and actually i've got the wrong one open because i've got them by the same name so using that id it should match up to the id that you've got here as the id that we just copied so once we open up and we can select it as the doc object there we can get the body and once we've got the body object that gives us an opportunity to write to it so selecting and we're going to assign it to a variable so that we can interact with that object and for now we'll just output the body object into the log so instead of making a dock we'll update the dock and so we've got the document body section and each one of these objects is going to have its own set of methods that you can use and for the dock itself we can rename so we can set a name for the dock so to avoid confusion i'll just write this one as the name for the dock so let's update the dock and we see that the name has now been changed for the stock that's associated with it and we can select the body and for here we've got a number of options where we could add list items we can add a paragraph add a table uh add an image add a horizontal rule so let's uh do that or append it with a horizontal rule and see what we end up with there so we looks like we're able to append it with a horizontal rule let's add a paragraph so within the body and append and append a paragraph and the parameter that it's expecting so it's expecting a string value so new para graph and that will add a new paragraph once we run the code so there's our new paragraph and we can combine where for instance if we wanted to add multiple paragraphs within a loop so we want to add 10 paragraphs let's add that paragraph there and i'll set up a value for the html so using the template literals we can update and we can have some content there and then whatever the value of x is at the time let's append it with the html content so update the doc and that added all of those paragraphs so it doesn't actually delete it's continues to add to the document so the more times we run the code the more it will add to that content so that's how you can create a document and you can also select the document and make updates to it within your google apps script
Info
Channel: Laurence Svekis
Views: 9,383
Rating: undefined out of 5
Keywords: Laurence Svekis, Svekis, Courses, Udemy, Learning, Coding, Learn code, JavaScript, HTML, CSS, JS code, Code example, elearning, Learn to code, how to code, learn web, web design, web development, application development, coding course, online course, responsive design, modern web design
Id: nVQKj1zcfak
Channel Id: undefined
Length: 6min 31sec (391 seconds)
Published: Tue Nov 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.