Automate Excel spreadsheet using App Script
I have a topic for you called App script . It is very wonderful JavaScript based technology made by Google and you can integrate with and automate tasks across Google products like Gmail Excel sheet, etc..
After learn this Topic you can easy make your Excel sheet fully Automated , Even you can configure a trigger based on specific times.Both technical and non-technical guys should find it very simple, which is my goal. sharing the code with you that will make it run at your end and give you the desired result. If you are familiar with the fundamentals of javascript and basic oops, you are a great .Hope you aware of google excel sheet and its row ,column , tabs and pivot table etc .
While you doing Automate your Excel sheet you have to decide the trigger times, API request, response and Excel Sheet 's features. I have no doubt that it simplifies your task and cuts down on manual work.
- First open your google excel sheet .
- Create new Tab and rename StudentData and you can change name as per your wishes.
- Go to the Extensions and click the App Script in menu of excel sheet.
- Now you are in App script Dashboard where you can see the Files, default code.gs ,Libraries and services in left of the menu bar on the other hand Run command top of the menu and dropdown has contained the listed function. Where OnOpen is a default function or you can make its own function as well.
// Add a menu button on opening of this sheet that calls the worker function.
function onOpen() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet()
var menuItems = [];
// menu and sub menu
menuItems.push({name: "Get bits Student", functionName: "GetReport"});
activeSheet.addMenu("API", menuItems);
}
// GetReport function that fetch the current 's data.
function GetReport() {
// Common variables.
const activeSheet = SpreadsheetApp.getActiveSpreadsheet()
const prDebugMsgs = true
const bitsSData = activeSheet.getSheetByName('StudentData'); // declare the tab name as StudentData
var url = "put your api url"; // put api url which retuen csv or comma-separated
var header = {
'cache-control': 'no-cache',
'Accept': 'text/csv; charset=UTF-8'
};
var options = {
"method" : "GET",
"header" : header
};
var results = UrlFetchApp.fetch(url,options);
bitsSData.clear({contentsOnly: true});
console.log(results.getResponseCode());
//parsing the comma-separated text
bitsSData.clearContents().clearFormats();
var arrJson=csvJSON(results);
var stringify = JSON.parse(arrJson);
bitsSData.appendRow(['Date','Name']); // Date and Name sample columns name
Object.keys(stringify).forEach(key => {
bitsSData.appendRow([stringify[key]['Sample1'],stringify[key]['Sample2']]); // Put your Json key and value accordingly
});
}
// Parsing the CSV data into json format
function csvJSON(csv){
var lines=csv.toString().split('\n');
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
//return result; //JavaScript object
return JSON.stringify(result); //JSON
}
Comments
Post a Comment