Wednesday, August 29, 2012

How to write a script in Google Spreadsheet

If you are new to Google Spreadsheets and you have never written a script before or if you don't know anything about it, then this tutorial will help you to know how you can write a script in a Google Spreadsheet.

Step 1) Open Script Editor:

From "Tools" menu, click on "Script editor..."

Look at the following screenshot:





This will navigate to you to the Script Editor.

Have a look at the screenshot below:




So, now you are ready to write the code in your Google Spreadsheet script editor. You may notice some code already exists on the script, you may delete it and start fresh with your own script.


Now if you want to print "Hello", then make a function in your script with whatever the name you like. I have made the function functionHello(), have a look at the following script:

function functionHello()
{
  Browser.msgBox("Hello");

}

Now, copy the above script in your script editor and try to run the function:



And then go to your spreadsheet. You will see the small pop-up window displaying message "Hello":



Hope this helps you to understand how to open script editor and start writing scripts in Google Spreadsheet.

12 comments:

  1. I love it! Thank you for this information!

    ReplyDelete
  2. Thanks it's very helpful.. well done.

    ReplyDelete
  3. Yes , very helpful.
    Ive wondered how to get into scripts for a while now, a little hello world way in is perfect.

    Thank you.

    ReplyDelete
  4. I'm trying to run a script for the first time. When I run the script I get a popup that says "Authorization Required" and then "Sheets List Code needs your permission to access your data on Google." Is this supposed to happen? I'm assuming it's just a security feature/setting but want to be sure.

    ReplyDelete
    Replies
    1. This just happened to me too. I think part of it might have been that I was not logged into any Google apps prior. Also a security measure when accessing another something else. So far I have not died from accepting it.

      Delete
  5. Uhhhh. These instructions do not look complete. Looks like when you opened the script editor you renamed it from Untitled Project to myScript. How did the function written in a project called myScript know to run in a spreadsheet named mySpreadsheet.
    does each seperate script get it's own file or can myScript hold several functions.
    How do I include the function in my cell formulas?

    ReplyDelete
    Replies
    1. My understanding is spreadsheets have names and scripts have their own names. One script can have multiple functions if necessary. I think scripts can reference and change cells as part of their function. Don't ask me how to do that though - I am just learning too.

      Delete
  6. This looks like what I need but it is very foreign to me can I share my google sheet with you for help. please I know I need to put info into the function you sent but what and where.

    ReplyDelete