Sunday, May 31, 2020

Google Apps scripts, features and gotchas

At work, we had an interesting problem to solve. There were surveys being sent out in google forms and we had to process the response before making the employees follow the next steps of action. When I started researching, I came across google apps scripts( script.google.com ) where one can code actions on scripts, sending emails and other such interesting things.

Following are the things upon which google scripts can be written:
1. Google forms
2. Spreadsheets
3. Documents
4. Integrating with third party APIs or any google API for that matter using OAuth

To start coding, one needs to know javascript as a pre-requisite. But apps script uses a specialized library which exposes classes and methods for different google suite products. Below are the ones which I got to explore:

  • SpreadsheetApp for reading/writing from/to google spreadsheet
  • FormApp for reading/writing from/to google forms. The only caveat of form is that on submit, we cannot preprocess the form before submit. One has to write an explicit trigger to trigger some code after submit. On initializing as well, one can write some code to set/ correct form data.
  • One can use Session variable of the apps script which can be used to get the current running user and  their basic information(like email, name and others).
  • UrlFetchApp to fetch response generally from a website or an API. Got to work with custom APIs which had Auth Token, use Google Spreadsheet, Google secrets manager API using Url FetchApp
  • Use custom libraries in your script like OAuth2. Use of things like OAuth ensures that we can do gsuite automation without the hassle of asking employees to authorize their access to google resources for the first time the script app runs.
  • HTMLService when we want to expose Google script as HTML output.
    Below is the sample code which does the same:
    function doGet() {
      return HtmlService.createTemplateFromFile('Index').evaluate();
    }
    The function doGet() is the entry point for HTML output. Here the function looks for a file called Index.html and loads it. One can create CSS, javascript and reference them in the Index page. But the Stylesheet and Javascript file have to be added as .html in the project
To deploy the project, one can just go to Publish and find the various options to publish the project.
One can deploy the project as 'Web app' which will create a script executable. This script executable can be referenced in a web page or a google site. The project will appear as a web frame.
For deployment as a forms addon, one can start coding by going to a google form. In the editable mode of the form, one can click on script editor and start coding for the form actions. 

Few things which can be taken advantage of:
The variables defined in the project will be accessible throughout the project in any file of the project. This brings me to the next best practice which is segregating functions in a different file so that it is easier to maintain.
Depending on which class is used, the scopes of the project gets updated automatically. If SpreadsheetApp is used, then the scope of https://www.googleapis.com/auth/spreadsheets is added.
This is later used by the application to challenge for authorization for the user to get required permissions for the script to run.


References:

Monday, March 13, 2017

Debugging Django applications using visual studio code

Visual studio code is the open source IDE from Microsoft which supports so many languages(C#,python, PHP)
Prerequisites to start debugging Django applications in Visual studio code-

  • Basic knowledge of visual studio or any other IDE
  • Basic knowledge of Django


Steps to debug the Django web application-

  1. Download visual studio code from here
  2. Create a django project if there is no project created.. Reference documenation here
  3. Open the visual studio code application
  4. Open a django project from folder
  5. Open  window Debug, set Django as the environment and click play button or press function f5 to start debug

Put breakpoint at any position to start debugging the code

Monday, August 16, 2010

Noteworthy blogs of my friends.

Been a while since I`ve updated my blog. Seeing my friends blogs, I have atleast got the drive to add another blog entry :)
Here`s the list:
Anurag Saurabh`s blog on general technology topics: Anurag`s stencil

Amal Hashim`s blog of MS technologies: .Net Goodies

Saturday, November 14, 2009

My experiences with IE8

I was a bit apprehensive to use the IE8 which created a buzz amongst developers and technology enthusiasts.
But I must say I am impressed by the sheer number of features supported by IE8. First and foremostly, MS had a re-look at the pathetic rendering engine
Trident and made significant changes in Trident which are noteworthy and are visible from the outset of the browser.

My favourite features of IE8:

  • The best feature I liked was to select a part of page and use it as a live bookmark:O Yep, its as easy as it gets!
  • The addons offered from IE are cool and there are quite a few of them.
  • Great security features with respect to the private browsing sessions provided and .
  • Inter translation of pages leveraging the globalization aspect of websites.
  • As a web developer who was to constantly work with colours of web pages, width of web page elements there s great news for you. Stay on a web page and just press F12 to open the trident rendering engine. Not only the tool shows details of the page, it also provides profiling of web pages. Added to this, there are tools like colorpix and cool ruler integrated which are handy for a website designer.
  • Pretty fast browsing speed.
  • Right click options of the browser are noteworthy although most of the right click options are tightly linked to windows live and other MS products like excel and one-note.

On the down side, few features of IE8 in particular and IE in general always bother me.

  • IE8 caches a lot of data. Infact IE8`s memory consumption is comparable to that of FF owing to the caching.
  • I hate the tick tick sound when a page gets rendered. Dont know how this issue has not been noticed my MS.

So, thus ends my experiences of the short stint with IE8.Comments and discussions on the same are always welcome :)

References:

  1. http://en.wikipedia.org/wiki/Trident_(layout_engine)
  2. http://www.microsoft.com/windows/internet-explorer/features/faster.aspx

Friday, August 7, 2009

PDF generation using .net

I had got a requirement from client to generate membership certificate as a pdf document for the person who joins the website.We researched and planned to make use a third party library called itextsharpLib.

The requirement was to generate a pdf which had a background image and some text to be placed into the pdf which was not that difficult a requirement and hence was able to finish it off within a day with help from the internet mind you.

The problem with this requirement was that there was very less documentation in the internet about the itextsharp library for .net.

Also we had planned to dynamically generate a web page and convert the HTML to pdf. But there was a lot of complexity in the requirement to generate the pdf using HTML. So, I took the easier route and generated the pdf required pretty easily.

So I refered this link to get the required tutorial needed for my requirement.

Steps I went through to get the requirement done:

  1. Instantiate the memory stream stream, required for holding the pdf document generated.
  2. Create a document object document, with the required page size and other layout options.
  3. Instantiate a PdfWriter object writer, which requires the memory stream and document as parameters.
  4. Open the document object using document.Open() method.
  5. Created the Image element and added it to the document using document.Add(img) where img is the obj of the type Image which would be implemented using the IElement interface. Prior to the adding we needed to have initialized the img properties.
  6. Created a PdfContentbyte object using the line PdfContentByte cb = writer.DirectContent;
  7. Write the content on the image between the lines cb.BeginText(); and cb.EndText(); using the method cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Text to be written", x-coordinate, y-coordinate,z-coordinate);
  8. Close the document object using document.close();
Now that we have the pdf document in the memory stream, putting the document in the HTTPHeader of the existing HTTPContext is pretty simple.
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
string test = "document";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+test+".pdf");
HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
HttpContext.Current.Response.End();

And there you are, you will get the pdf document as an attachment in your browser.

P.S: The blog entry is under construction. Will add the code snippet and downloadable source code shortly.

Thursday, August 6, 2009

My favourite sites and tools.

I am addicted to the internet and am obviously fascinated by the way in which the websites have evolved and so also the content. In this blog, I am going to list out a few of my favourite sites which I regularly go through to update myself with the happenings in the world.

I always believe that if you want to search anything on the internet, be it books, information or anything which you are interested in, try to see only from authentic sources. For example, if its book you are researching on, see the review of the book in amazon.com. You will also get related links related to the book that you have searched on amazon.

Below are the list of the few of my favourite sites

Technology related:
  • http://blogs.msdn.com/default.aspx - is the blog on Microsoft Technologies which I am interested in and I regularly go through
  • http://www.gartner.com/ is the blog which tells about the current trends in IT.
  • http://www.w3schools.com/ is the website to learn basic website building skills.
  • http://www.uxunleashed.com/ about the various user experience related blog. This blog is created by the previous organisation where I used to work.
  • http://www.readwriteweb.com/ which gives the latest technology trends of the world.
  • http://www.apifinder.com/ to search for any APIs that are available for free which might be needed for solving a problem.
Hobbies:
  • htttp://www.imdb.com has the complete list of hollywood and foreign language movies
  • http://www.the-numbers.com gives the list of hollywood movies box office performance.
  • http://www.livemocha.com if a person is interested in learning a foreign language online.
P.S: The list will be evergrowing as the web is ever growing. So will update this blog regularly. Also, there might be better sites than what I have mentioned in the blog entry. Suggestions and comments are always welcome.