Monday, May 14, 2018

Java - Microsoft Office




We all know about Microsoft Office products and its capabilities. For a developer, he/she uses Microsoft Word for document purpose like design specifications, technical specification etc., Microsoft Excel for reporting and Microsoft Powerpoint for presentations. Can a developer write code to manipulate data in Microsoft Office Documents? Or do the Microsoft Office products have the extensibility to use with programming language like Java?
 
Apache POI has the solution, it’s the Java API for Microsoft Documents. In this post, we will look a sample code to manipulate data in Microsoft Excel using Java.

For the Java project, we create a maven java project by skipping the archetype and add the following dependency in the pom.xml of the project,

  

ArtifactId version is 3.12 but 3.15 version is available now but there are some methods deprecated so, i am comfortable with using 3.12 version.


Excel Task - Well when I was studying Microsoft Excel in 2001, we usually do an exercise like writing an excel function to grade marks. Here is a sample below,




So, my java code have to read an Excel File and have to grade Marks value by writing back to Excel File. The below image depicts the reading file data,


The whole code is available at GitHub   https://github.com/meharuban/ApachePOIExcel

I will explain some fragments of the code,

private static final String FileName = "Grade_Report.xlsx";

Excel file name to be read and write.

FileInputStream excelFile = new FileInputStream(new File(FileName));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator iterator = datatypeSheet.iterator();

The above code reads the Microsoft Excel WorkBook file and reads the first sheet.

The Grading happens here,

            for (StudentModel model : modelList) {
                                    if (model.getMarks() > 75) {
                                                model.setGrade("A");
                                    } else if (model.getMarks() > 50) {
                                                model.setGrade("B");
                                    } else if (model.getMarks() > 30) {
                                                model.setGrade("C");
                                    } else {
                                                model.setGrade("F");
                                    }
                                    modelGrade.add(model);
                        }

Finally writing back to Excel File,

FileOutputStream outputStream = new FileOutputStream(FileName);
workbook.write(outputStream);
workbook.close();

Hope everyone will understand the code because it’s simple and not much logic here. I would like to write more on Java and JavaScript. Please expect future posts.

Saturday, March 31, 2018

Service Account Keys - Google Translation API


In my last post, I have mentioned about Google Translation API and how to invoke the api using GCP Platform console and via Postman using API Keys. Now, we will see how to use in our Java code by using Service account keys. To use Service Account Keys, we need to install the client library. Following dependency need to add to the pom.xml file of our project,


In GCP Console when we create a service account key, a JSON file which contains the service account key will be downloaded to our computer. The next step is to add the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key as below,




Here is a sample code to test the translation,


import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class TranslationApi {
            public static void main(String... args) throws Exception {
                        // Instantiates a client
                        Translate translate = TranslateOptions.getDefaultInstance()
                                                .getService();
                        String text = "Hello";

                        // Translates some text into Chinese (Traditional)
                        Translation translation = translate.translate(text,
                                                TranslateOption.sourceLanguage("en"),
                                                TranslateOption.targetLanguage("zh-TW"));

                        System.out.println("Word in English - " + text);
                        System.out.println("Word in Chinese (Traditional) - "
                                                + translation.getTranslatedText());

            }
}

The Console Output will be,

Word in English - Hello
Word in Chinese (Traditional) - ??

In Eclipse console, chinese characters are displayed as “??..” by default. To display the chinese characters, we need to change the run configuration as follows,


After the above change, the console output will be,

Word in English - Hello
Word in Chinese (Traditional) - 你好

Expect Future Posts, Thank You.

Tuesday, March 27, 2018

Google Translation API


When you want to translate words from one language to another language, What will you do? Go to google translate and select the language origin and the target language as described below,





What if you want to translate in your Java code? For this Google has a solution in thier Google Cloud Platform(GCP), Google Cloud Translation API. In GCP platform, Google provides their api’s and in the platform you can do the api analytics and billing! Forget to mention its not free but for initial signup there are giving $300 credits free and we can use for 1 year.

Let’s look at the Google Cloud Translation API,



In the translation API, there are five methods listed below,




For the translation, we need to use language.translations.translate method. 

In the GCP platform we can test the api as follows,


For the “q” property, we need to type the text to be translated.

For the “source” property, we need to give the text language origin
For the “target” property, we need to give the target language.

The below image depicts the api call result,



So, we have tested the api using GCP platform, how about testing the api from externally

So, here comes POSTMAN,



Postman is an api testing tool which can be installed in Google Chrome as a plugin. To test the api, Google needs to authenticate us, so Google provides authentication mechanism like API Key, Oauth 2.0 Client ID’s.  As i earlier mentioned, its not free so we need to sign up for billing using our credit card and can use the free credits.

To get the API Key, we need to create a project in GCP platform and have to generate the key,
The api can be invoked by calling,

The Postman result follows,



Expect Future Posts, Thank You

Monday, May 17, 2010

Status Report 1 –Google Summer of Code 2010


Project: Improved automated UI testing for Lightning

Organization: Mozilla                                                                          
Mentor: Merike Sell
In my GSOC project, this time period is allocated for community bonding period but I thought to start the project to some extent. Week before last week, I couldn’t do anything because of my internship & other stuffs. In this status report, I am writing what I did in last week & what I have to do in this week.
Last Week…….
Last week, I learned the following regarding my project
  • Mozmill(Test Frame Work, Making Useful Tests, Elements Library Objects)
  • Mozmill Test Documentation
  • SVN,CVS(Revision Control systems)
  •   Shared Modules
  • Mercurial
  • Merge Program
  • Got Familiar with DOM Inspector
Installed Visual C++ 9.0 Express Edition, Mozilla Build Package, Mozmill Command Line Client
What is Mozilla Build Package? 
Mozilla Build is a bundle of software including of bash, GNU make, autoconf, Mercurial, and much more.
Mercurial: - Mercurial is a free, distributed source control management tool. It efficiently handles projects of any size and offers an easy and intuitive interface.
This Week…….
  • Getting a depth knowledge of DOM Inspector
  • Learning how to check out the repository
  • Participating in Mozilla IRC
  • Discussing Buildbot related issues.


References

Sunday, May 2, 2010

Google Summer of Code 2010





“University of Moratuwa becomes World’s Hot Spot” was the words I heard when University of Moratuwa got highest number of accepted student proposals in Google Summer of Code(GSOC) 2008.The above statement is not only applicable for 2008 & it continued in 2009 and again now in 2010 as well.  In GSOC 2010, University of Moratuwa got 26 accepted Students proposals and I am very happy to say that I am also a student of University of Moratuwa who got accepted for Google Summer of Code 2010.

Out of those 26 students 22 are from Department of Computer Science & Engineering which is my department and 12 out of those 22 were obtained by my CSE 07 Batch. Furthermore Electronic & Telecommunication Department got one & Faculty of Information Technology got 3.

My project is “Improved automated UI testing for Lightning”. Lightning brings the Sunbird calendar to the popular email client, Mozilla Thunderbird and the SeaMonkey internet application suite (SeaMonkey is a free and open source cross-platform Internet suite).
I want to thank Merike Sell who is the mentor for my project & Calendar lead developer Philipp Kewisch who is willing to help me in this project. I also want to thank Ms. Vishaka Nanayakkara Head of the Department of Computer Science & Engineering, who motivated CSE students to participate in Google Summer of Code.

I am very happy to do this project & want to have a good experience.