Google Doc Scripting!

If you want to add additional functionality for google docs you can simply write scripts.

Google provides various API to accomplish this, eg Google Spreadsheet API, Google DocList API etc.


Here is an example script that copies a "template file" and adds collaborators that are specified in another spread sheet (list)



function myFunction() {

var templateFile = DocsList.getFileById("tvkH8axayF4-GSXWpzZ7oSQ"); //id of the template
var listFile = DocsList.getFileById("tyAQs1cGxA1MhLj462e9d2A"); //id of collaborators

var listSpreadSheet = SpreadsheetApp.open(listFile);
var listSheet = listSpreadSheet.getSheets()[0];

var listRange = listSheet.getDataRange();


var values = listSheet.getSheetValues(1, 1, listRange.getLastRow(),listRange.getLastColumn());

for(row=1; row < listRange.getLastRow(); row++){

var name = values[row][0];
if(name == ""){
continue;
}

//create new file
var newFile = DocsList.copy(templateFile, name+" Feedback");

var spreadSheet = SpreadsheetApp.open(newFile);


//get the url of the attached form of the copied spreadsheet
var formURL = spreadSheet.getFormUrl();


var collaborator = values[row][1];


if(collaborator != "" || collaborator!= undefined|| collaborator!=null){
spreadSheet.addCollaborators(collaborator,{editorAccess:false, emailInvitations:false}); /*you can set emailInvitatins:true if you want to send emails to collaborators */

}
listSheet.getRange(row+1,3).getCell(1,1).setValue(formURL);
}


SpreadsheetApp.flush(); //this is important for changes to take effect

}​

No comments: