I am trying to add a menu to a newly created Spreadsheet using Google App Script.
I don't know if that is even possible.
Can I somehow automatically add a script to a new Spreadsheetfile where i can define the onOpen() method?
Or can I add the menu from the outside of the Spreadsheet-file, where I have created the file itself?
My code looks like this:
sheetfile = SpreadsheetApp.create(sheetName);
sheetfile = DriveApp.getFileById(sheetfile.getId());
sheetfile = SpreadsheetApp.open(sheetfile);
var menuEntries = [];
menuEntries.push({name: "Choose Folder", functionName: "showPicker"});
sheetfile.addMenu("Export to JSON-file", menuEntries);
But this does not work.
Your help is very much appreciated! :D
You can't insert a script directly when creating a new spreadsheet.
The best way to get this effect is to create template spreadsheet with the necessary script (with the onOpen & your other functions) attached, then create a copy of that template sheet instead of creating a brand new sheet.
When you create the copy, the script will be carried over along with any contents in the sheet.
var template_sheet = SpreadsheetApp.openByUrl('YOUR TEMPLATE SHEET URL');
var sheetfile = template_sheet.copy(sheetName);
Related
How to automatically create a new Google Spreadsheet with Script Editor?
I know it seems obvious but I couldn't find an answer to this as everyone has only been interested in creating a sheet, instead of an entirely new spreadsheet automatically.
To create anew spreadsheet refer to the create() method there : https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#createname
Code is really simple
function createNewSpreadsheet(){
var ssNew = SpreadsheetApp.create("My New Spreadsheet");
Logger.log(ssNew.getUrl());
}
And if you want to get the first tab
var firstTab = ssNew.getSheets()[0] ;
And if you want to get the cell C15
var cell = firstTab.getRange("C15");
Stéphane
Receiving the error,
ReferenceError: "setActiveSelection" is not defined.
Using Google Apps Script on Google Sheets. With this function, I'm wanting to create a new sheet tab on the same workbook using a template from another workbook. Once the new sheet is created, I'd like to change the actively selected cell to cell 'B1'. The template is copying fine, but the error is coming from the line changing the active selection. I've also tried setActiveRange and even just setActiveSheet but they return the same error.
function newReception() {
var templateSpreadsheet = SpreadsheetApp.openById("ID Redacted");
var template = templateSpreadsheet.getSheets()[1];
var currentSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = template.copyTo(currentSpreadsheet).setName("Untitled
Reception");
setActiveSelection(currentSpreadsheet.getSheetbyName("Untitled
Reception").getRange('B1'));
}
Please advise on a solution. Thanks!
You're getting this issue because you're running .setActiveSelection() on its own. This method is bound to sheets, so you must run it for such. See Class Sheet documentation for more details on this method.
Try defining your sheet as a variable and then running it for your new sheet variable.
var sheet = currentSpreadsheet.getSheetbyName("Untitled Reception");
sheet.setActiveSelection('B1');
I was wondering if someone could offer me some advice.
I have a master spreadsheet, acting as a template. I have written a script which can be run from the menu (using addToUi command), which makes a copy of template spreadsheet.
The problem is that the script gets copied into the new spreadsheet also which I don't want.
Could anyone suggest a possible way around this please?
I did think a possible way was to get the script to open the copied template and delete the script but not sure if this is possible.
Here is the function which does the copying....
function createCopy() {
var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("B8").getValue();
var destinationFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxx");
DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(myValue,destinationFolder);
}
(Cell reference B8 holds the value of what I called the copied spreadsheet).
Rayden, I use a function like that to just copy one sheet to a new spreadsheet and it doesn't drag the script with it. gDrive is the id for the Spreadsheet, tabName the individual sheet you want copied, Filename the name of the copy and destination the destination directory.
//******************************************************************************
//- This function takes a tab and makes it its own file
function tabToSheet(gDrive,tabName,fileName,destination){
var sh = SpreadsheetApp.openById(gDrive);
var ss = sh.getSheetByName(tabName);
//create a new document in the location given
var newSheet = SpreadsheetApp.create("TEMPDELETEME");
//copy the tab from current document to new document
ss.copyTo(newSheet);
var id = newSheet.getId();
newSheet.deleteSheet(newSheet.getSheetByName("Sheet1"));
os = newSheet.getSheets()[0];
os.setName(tabName);
var file = DriveApp.getFileById(id);
var folder = DriveApp.getFolderById(destination);
var finalId = file.makeCopy(fileName, folder).getId();
file.setTrashed(true);
return finalId;
}//*****************************************************************************
The difference is that i'm making a new sheet, then copying the tab rather than copying the entire file. You could add another tab and remove the variables if you want to copy multiple tabs.
I was having trouble implementing J.G.'s approach of moving individual sheets, so the approach I took was to add a conditional in front of the script to only run if the spreadsheet id was equal to the id of the original spreadsheet. My use case was trying to suppress a custom menu on the original workbook from reappearing on the copy, so it worked for that.
var bookId = spreadsheet.getId();
if (bookId === 'original spreadsheet id') {
*Function*
}
Currently I have a google spreadsheet document in which users are inputting data based on conditional formatting the cells are changing colors. I would like to create a button with which I can easy restore all the cells into their first state.
I have this script at the moment:
function clearRange() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('B13:B30').clearContent();
}
Which works like a charm but the thing is that I have default data in the cells and when this scripts get applied it clears the default data.
Any ideas how to make such a script which will clear only the inputted data by the user.
It's not the solution you want but apart from what Scott suggested, which is the most straightforward approach, you could make a copy of your sheet in the unedited state. In this copy have a script which will just copy and paste the info into the sheet that the user fills in.
Then if you make changes you just need to update the template sheet and not the script.
This is a very basic script but should you'll get the idea.
function myFunction() {
var app = SpreadsheetApp;
var tempSheet = app.getActiveSpreadsheet().getSheetByName('Sheet1');
var inputSheet = app.openById('-----ID OF INPUT SHEET----').getSheetByName('Sheet1');
var tempData = tempSheet.getDataRange().getValues();
inputSheet.getRange(1, 1, tempData.length,
tempData[1].length).setValues(tempData);
}
This is a question about a Google docs scripting document.
I would like to create a script that, when I run it, will transfer one row of data from a questionnaire (that I choose before running the script) to a completely new google docs word document.
Is this possible, and if so how would I do it?
Shouldn't be too difficult - tricky bit comes with what you want to do with the data in the new doc. Example script to do this would be:
function sendToNewSheet() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
var range = sheet.getRange(row,1,1,sheet.getLastColumn()).getValues();
var doc = DocumentApp.create(new Date().getTime());
doc.appendParagraph(range.join());
doc.saveAndClose();
}
function onOpen() {
var subMenus = [{name:"Send to new sheet", functionName: "sendToNewSheet"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Help Desk Menu", subMenus);
}
With that, you can select a cell in your spreadsheet and then use the menu to execute a function that will take the data in that cell's row, create a new document, and add the data into that document. If you wanted to name the document based on the data, you could do:
var doc = DocumentApp.create(range[0][1]);
Which would use the data in the second column. The document is created in your top level directory but you can move it after it's created into a specific folder you want.