Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//Create menu to run examples from
function onOpen(){
SpreadsheetApp.getUi()
.createMenu("Examples")
.addItem("example 1", "example1")
.addItem("example 2", "example2")
.addItem("example 3", "example3")
.addItem("example 4", "example4")
.addItem("example 5", "example5")
.addItem("example 6", "example6")
.addItem("example 7", "example7")
.addToUi();
}
//Global variables
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Create a new spreadsheet
function example1() {
SpreadsheetApp.create("New Spreadsheet1");
}
//Create a new spreadsheet with 20 rows and 10 columns
function example2() {
SpreadsheetApp.create("New Spreadsheet2", 20, 10);
}
//Create a new spreadsheet with a name from a sheet
function example3() {
var name = sheet.getRange("A1").getValue();
SpreadsheetApp.create(name);
}
//Create multiple spreadsheets with different names from a sheet
function example4() {
var names = sheet.getRange("B1:B3").getValues();
for (i in names){
SpreadsheetApp.create(names[i]);
}
}
//Get a value from one spreadsheet and add it into another, using it URL
function example5(){
var text = sheet.getRange("C1").getValue();
var newSS = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1xFwNEaBjvHeBfxW_z4wBsyozedn5sQ82FWsiv-J5aEI/edit#gid=0");
newSS.getActiveSheet().getRange("A1").setValue(text);
}
//Get a value from one spreadsheet and add it into another, using it ID
function example6(){
var text = sheet.getRange("C1").getValue();
var newSS = SpreadsheetApp.openById("1xFwNEaBjvHeBfxW_z4wBsyozedn5sQ82FWsiv-J5aEI");
newSS.getActiveSheet().getRange("A2").setValue(text);
}
//Create multiple spreadsheets with different names and different pieces of text from a sheet
function example7(){
var ssNames = sheet.getRange("D1:D3").getValues();
var texts = sheet.getRange("E1:E3").getValues();
for (i in ssNames){
var spreadsheet = SpreadsheetApp.create(ssNames[i]);
spreadsheet.getActiveSheet().getRange("A1").setValue(texts[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment