Skip to content

Instantly share code, notes, and snippets.

@barrieroberts
Created May 18, 2017 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barrieroberts/8e77c584b02d4c41b9579e99ef03bd61 to your computer and use it in GitHub Desktop.
Save barrieroberts/8e77c584b02d4c41b9579e99ef03bd61 to your computer and use it in GitHub Desktop.
//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