Category Archives: Automation / Scripting

Google docs – send a doc monthly via email

Here is a small script you can attach to a document on Google docs.

Whenever run, it will attach the file and send it to you via e-mail

function monthlyBackup() {
  var formattedMonth = Utilities.formatDate(new Date(), "GMT", "MM");
  var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss"); var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " backup " + formattedDate;
  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId(); var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName(); //var email = Session.getUser().getEmail(); var email = "XXX@XXX.XXX";
  var subject = SpreadsheetApp.getActiveSpreadsheet().getName() + " backup at " + formattedDate;
  var body = "This is automated backup from google docs :)";
  var requestData = { "method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()} };
  var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=ods&id="+ssID;
  var result = UrlFetchApp.fetch(url , requestData);
  var contents = result.getContent();
  var attachedFilename = sheetName + " " + formattedMonth + ".ods";
  MailApp.sendEmail(email, subject ,body, {attachments:[{fileName:attachedFilename, content:contents, mimeType:"application//x-vnd.oasis.opendocument.spreadsheet"}]}); }

 

For more MIME types read this article:

https://developers.google.com/drive/v3/web/integrate-open

If you want to add a menu item you can also the following code:

 

function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var menuEntries = [ {name: "Send Email", functionName: "monthlyBackup"}];
 ss.addMenu("Scripts", menuEntries);
};

 

References:

http://igoogledrive.blogspot.gr/2012/08/send-google-spreadsheet-sheet-to-email.html

https://webapps.stackexchange.com/questions/60445/send-an-email-with-attachment-using-google-apps-script

https://developers.google.com/drive/v3/web/integrate-open

Bash history expansion 🏅

The general syntax is:

!command:parameter

Command segment defines which command to look for in history. It can be a modifier like !! (last command), !-1, !-2, !-3 … (last, previous to last etc) or a specific keyword like !cat (search for most recent cat).

Parameter segment defines which part of the command to use. ^ is first parameter, $ is last parameter, * are all the parameters, 0 the command and 1,2,3… are the parameters. You can define a range eg 2-4 (parameters 2 to 4)

Besides that, you can also use ^x^y^ to replace x with y. There are some more advanced ways to search. Take a look at the references at the end of this page.

You can also use history n to review the last n commands with their ids. If you know an id, you can use it at command segment,

All results are shown as if the command was executed immediately after touch a b c d e (not one after another)

Assume that you have typed in this order:

  1. ls -a -r
  2. date
  3. touch a b c d e
!! last command (all words) touch a b c d e
!* last command, all arguments a b c d e
!^ last command, second argument a
!$ last command, last argument e
!:0 first word touch
!:1 second word (first argument) a
!:2-4 range of words b c d
 ^touch^ls find touch and replace it with ls  ls a b c e d
!-1 last command touch a b c d e
!-2 previous to last command date
!-2:0 first keyword of previous to last command date
!ls:* search for command ls,
all arguments of ls
-a -r
!ls:^ search for command ls,
first argument of ls
-a
!ls:$ search for command ls,
last argument of ls
-r
!ls search for command starting from ls ls -a -r
!? b search for command containing b touch a b c d e
!!:gs/a/p/ last command, replace a with b touch p b c d e

References:

http://www.thegeekstuff.com/2011/08/bash-history-expansion

https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html#Event-Designators

Book recommendation:

UNIX Shells by Example, Ellie Quigley