Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Google App Script Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Putting Value in the spreadsheet through Google App script

function myFunction()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var range=sheet.getRange('A1');
range.setValue('Hello')
}

SEND TEST E-MAIL


function myFunction() {

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var range=sheet.getDataRange();
var values = range.getValues();

MailApp.sendEmail('ashish.khurana@wadi.com', 'test mail', 'test');


}

SEND TEST E-MAIL BY EXTRACTING DATA FROM SPREADSHEET


function myFunction() {

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var range=sheet.getDataRange();
var values = range.getValues();
var email=values[1][0];
var subject= values[1][2];

var body=values[1][3];

MailApp.sendEmail(email, subject, body);


}
SENDING EMAIL TO MULTIPLE PEOPLE
function myFunction() {

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var range=sheet.getDataRange();
var values = range.getValues();

for(i=1;i<values.length;i++)
{
var email=values[i][0];
var subject= values[i][2];
var body=values[i][3];

MailApp.sendEmail(email, subject, body);


}
}

SENDING MULTIPLE TEXT INTO BODY TO EMAIL


function myFunction() {

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var range=sheet.getDataRange();
var values = range.getValues();

var email=values[1][0];
var subject= values[1][2];

var data = sheet.getRange('A8:N40').getValues();


var body = '';
for( var row in data ) {
for( var col in data[row] ) {
body += data[row][col] + '\t';
}
body += '\n';
}
MailApp.sendEmail(email, subject, body);

LOOPING IN GOOGLE APP SCRIPT


function myFunction() {

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var range=sheet.getDataRange();
var values = range.getValues();
for(i=1;i<values.length;i++)
{
var email=values[i][0];
var subject= values[i][2];
var body=sheet.getRange('A1);

MailApp.sendEmail(email, subject, body);

}
function runDemo() {
try {

var firstProfile = getFirstProfile();


var results = getReportDataForProfile(firstProfile);
outputToSpreadsheet(results);

} catch(error) {
Browser.msgBox(error.message);
}
}

function getFirstProfile() {
var accounts = Analytics.Management.Accounts.list();

if (accounts.getItems()) {
var firstAccountId = accounts.getItems()[0].getId();

var webProperties = Analytics.Management.Webproperties.list(firstAccountId);


if (webProperties.getItems()) {

var firstWebPropertyId = webProperties.getItems()[0].getId();


var profiles = Analytics.Management.Profiles.list(firstAccountId, firstWebPropertyId);

if (profiles.getItems()) {
var firstProfile = profiles.getItems()[0];

return firstProfile;

} else {
throw new Error('No views (profiles) found.');
}
} else {
throw new Error('No webproperties found.');
}
}
Browser.msgBox(firstProfile) ;
/*
else {
throw new Error('No accounts found.');
}
*/
}

function getReportDataForProfile(firstProfile) {

var profileId = firstProfile.getId();


var tableId = 'ga:59791098';
var startDate = getLastNdays(14); // 2 weeks (a fortnight) ago.
var endDate = getLastNdays(0);

// Today.

var optArgs = {
'dimensions': 'ga:keyword',
'sort': '-ga:sessions,ga:keyword',

// Comma separated list of dimensions.


// Sort by sessions descending, then keyword.

'segment': 'dynamic::ga:isMobile==Yes', // Process only mobile traffic.


'filters': 'ga:source==google',

// Display only google traffic.

'start-index': '1',
'max-results': '250'

// Display the first 250 results.

};

// Make a request to the API.


var results = Analytics.Data.Ga.get(
tableId,

// Table id (format ga:xxxxxx).

startDate,

// Start-date (format yyyy-MM-dd).

endDate,

// End-date (format yyyy-MM-dd).

'ga:sessions,ga:pageviews', // Comma seperated list of metrics.


optArgs);

if (results.getRows()) {
return results;

} else {
throw new Error('No views (profiles) found');
}
}

function getLastNdays(nDaysAgo) {
var today = new Date();
var before = new Date();
before.setDate(today.getDate() - nDaysAgo);
return Utilities.formatDate(before, 'GMT', 'yyyy-MM-dd');
}

function outputToSpreadsheet(results) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();

// Print the headers.


var headerNames = [];
for (var i = 0, header; header = results.getColumnHeaders()[i]; ++i) {
headerNames.push(header.getName());
}
sheet.getRange(1, 1, 1, headerNames.length)
.setValues([headerNames]);

// Print the rows of data.


sheet.getRange(2, 1, results.getRows().length, headerNames.length)
.setValues(results.getRows());
}
------------------------------------------------------------------------------------------------------------------------------------------------wadimcc@wadi.com
google@wadi123

LINKING GOOGLE APP SCRIPT TO MYSQL


function runDemo() {

var connection = Jdbc.getConnection("jdbc:mysql://52.74.64.103:3306/wdview", "reporting",


"9lyeplzQl57G");
var SQLstatement = connection.createStatement();
var result = SQLstatement.executeQuery("SELECT * FROM status_cache");

var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getRange('A2');

// loop through result object, setting cell values to database data


var row = 0;
while(result.next()) {
for(var i=0; i<4; i++) {

// four fields per record

cell.offset(row, i).setValue(result.getString(i+1));
}
row++;
}

result.close();
SQLstatement.close();
connection.close();
}
Clear the contents of the spreadsheet
function runDemo() {

var ss=SpreadsheetApp.getActiveSpreadsheet();
var x=ss.getActiveSheet();
x.clearContents();

You might also like