I just want a "hello world" and after an embarrassing number of hours, its still not working. I'm new to Javascript and Google App script.
Here is the simple script that fails to enter the "Hello" and I would appreciate any tips you have on getting this to work.
As you can see in the commented lines, I've tried many ways to get my "Hello" out there, but none of them work yet. The menu does work. Thanks for your help
Really what I want to do is to be able to move from cell to cell in a spread sheet, get the values there, assign it to a variable. And then to write the var value to a cell after a condition is meet in the script.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Generate Coin Trades', functionName: 'GCTrades_'}
];
ss.addMenu('CoinTrade', menuItems)
function GCTrades_() {
var ss = SpreadsheetApp.getActive();
SpreadsheetApp.setActiveSheet(ss.getSheetByName('Prices'));
sheet.getRange("$H$1").setValue('Hello');
//settingsSheet.activate();
// One before the last row that has been entered in the spreadsheet
// var LastRow = sheet.spreadsheet.getLastRow() -1;
// var cell1 SpreadsheetApp.getActiveSheet().getRange(LastRow,16);
// var cell = spreadsheet.getRange("$H$1");
// cell.setvalue('Hello')
//var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// var cell = sheet.getRange(1655,16);
// var cell = 'H1655'
// cell.setValue("Hello");
//var count = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4)
// Spreadsheet.getActiveSheet().getRange(LastRow,16).setvalue('Hello')
// SpreadsheetApp.getActiveRange().setValue('hello')
}
There are a couple of typos preventing your script from running. First, your onOpen script isn't closed.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Generate Coin Trades', functionName: 'GCTrades_'}
];
ss.addMenu('CoinTrade', menuItems)
} // missing closing curly brace.
You used sheet.getRange()... but didn't define sheet anywhere. You also do not need to use setActive to edit the sheet, as you can do that on a cell-by-cell basis. It is enough to define the sheet in the script.
Once your sheet is defined, you can get ranges and set the values. I kept your absolute range ($H$1) for the first cell. We then use a relative reference for the second cell by getting the position using the .getRow() and .getColumn() methods.
function GCTrades_() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1')
var cell1 = sheet.getRange("$H$1").setValue('Hello');
var cell2 = sheet.getRange(cell1.getRow(), cell1.getColumn()+1).setValue('World');
}
Try this:
function onOpen()
{
SpreadsheetApp.getUi().createMenu('CoinTrade')
.addItem('Generate Coin Trades', 'GCTrades_')
.addToUi();
}
//This is a private function so it will not show up in script editors function list. If you want it to show up on the function list then remove the trailing underscore
function GCTrades_()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Prices').activate();//The activate is not required but it will force this sheet to be on top
sh.getRange("A1").setValue('Hello');//Do not use $s in the range here
}
What I really want:
Really what I want to do is to be able to move from cell to cell in a
spread sheet, get the values there, assign it to a variable. And then
to write the var value to a cell after a condition is meet in the
script.
It's not clear to me what you "really want". Perhaps, you can begin that code yourself and refine the description of what you "really want".
Related
In the following code snippet there is a very simple code.
When I try to use it I'm getting
ReferenceError: ADDRESS is not defined error.
I'll appreciate very much if someone could shed a light on this issue.
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange(ADDRESS(15,D1)).activate;
};
Issue 1:
ADDRESS is a google sheets function but you are trying to use it in Google Apps Script.
The latter does not accept google sheets formulas. It has its own documentation and only JavaScript is supported.
Issue 2:
To execute a function in any programming language you need to add parentheses at the end of the function. Replace activate with activate().
Solution:
You most likely want to take the range of cell D15. If that's the case then simply do:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D15').activate();
};
Keep in mind this will work for the active sheet only (the sheet that is currently selected). If you want to select a specific sheet by its name, then do that and change Sheet1 to the sheet name of your choice:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Sheet1') // put the sheet name of your choice
sheet.getRange('D15').activate();
};
Updated answer based on your comment:
Assuming cell D15 contains a cell reference of the cell you want to activate, then do that:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Sheet1') // put the sheet name of your choice
sheet.getRange(sheet.getRange('D15').getValue()).activate();
};
or based on your original code:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange(spreadsheet.getRange('D15').getValue()).activate();
};
I have this script which I found here which mimicks the 'make a copy' function, but also renames the sheet all within a single action.
function onOpen() {
var menu = [{name: "Duplicate and name", functionName: "dupName"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
}
function dupName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var name = Browser.inputBox('Enter new sheet name');
ss.insertSheet(name, {template: sheet});
}
I want to take this script a step farther and use it to increase the number in the name of the previous sheet, by 1.
For example, A sheet will be called 'Sheet 1' and when I duplicate the sheet using this script, I want the new one to be called 'Sheet 2'.
Is this possible?
If so, how can I modify this script to do that?
Or is there a better way to write it in the first place?
(considering the post I'm referencing to is 4 years old).
The following script can duplicate the sheets and also rename the same with your custom name and add protection to either selected range or complete sheet as defined in the protection line:
function script() {
var spreadsheet = SpreadsheetApp.getActive();
var myValue = SpreadsheetApp.getActiveSheet().getSheetName();
spreadsheet.duplicateActiveSheet();
var totalSheets = countSheets(); //script function
myValue = "CUSTOMTEXT" + totalSheets; //sample CUSTOMTEXT1
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
var protection = spreadsheet.getActiveSheet().protect();
protection.setUnprotectedRanges([spreadsheet.getRange('C2:E5')])
.removeEditors(['user1#gmail.com', 'user2#gmail.com']);
protection.addEditor('user0#gmail.com');
SpreadsheetApp.flush();
};
function countSheets() {
return SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
}
If the same function is triggered using the doget(e) function you can be the owner of the sheet irrespective to the triggering user. Kindly follow the following for more details on it: Changing Owner of the Sheet irrespective of the duplicator
You can use this script:
function onOpen() {
var menu = [{name: "Duplicate and name", functionName: "dupName"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
}
function dupName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var array = sheet.getName().split(" ");
var ssnum = Number(array[array.length-1]);
array[array.length-1] = ++ssnum;
ss.insertSheet(array.join(' '), {template: sheet});
}
Note that you need to have a space delimiter between the text part and the number part of the sheet name in order to use this script.
Sample Spreadsheet:
I have a sheet where when I change a specific cell to "YES", I need a template sheet to be copied to a new version and named as per the value of a cell on the current row.
I'm having trouble working out how to get the value of the first cell in the row selected. This is what I have so far (I know this is wrong):
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var currentCell = sheet.getCurrentCell();
if (currentCell = "YES")
{
SpreadsheetApp.getActiveSpreadsheet().toast("New change control sheet added to workbook.","Change Control",15);
var sourceRow = ss.getActiveRange().getRowIndex();
var tabName = ss.getRange(cell,1).getValues();
ss.getSheetByName("CCTemplate").showSheet()
.activate();
ss.setActiveSheet(ss.getSheetByName('CCTemplate'), true);
ss.duplicateActiveSheet();
ss.setActiveSheet(ss.getSheetByName('CCTemplate'), true);
ss.getActiveSheet().hideSheet();
ss.setActiveSheet(ss.getSheetByName('Copy of CCTemplate'), true);
ss.getActiveSheet().setName("CC" & tabName);
}
}
Any ideas?
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=='Your Sheet Name' && e.value=="YES") {
e.source.toast="New change control sheet added to workbook.","Change Control",15);
var tabName=sh.getRange(e.range.rowStart,1).getValue();
var tsh=e.source.getSheetByName('CCTemplate');
var csh=tsh.copyTo(e.source);
csh.setName('CC'+tabName);
}
}
You should avoid using activate in your scripts especially in simple triggers where you have to finish in 30 seconds. I think this code does the same thing that you intended for your code. One significant difference is that I use the information that comes in the event object that comes with the trigger. You should add the code Logger.log(JSON.stringify(e)) and then look at the logs you will see that there is a lot of information available to you which removes the need to run extra functions to get things like a spreadsheet.
Use event objects
onEdit offers among others the event objects range and value which are helpful to retrieve the range that has been edited and its value.
Also
When you want to a cell and compare it against a value, like in if (currentCell = "YES") - you need to retrive its value (either currentCell.getValue() or just event.value) and you need to use == instead of = for comparison.
Be careful with getValues() vs getValue(). The former gives you a 2D array and is not necessary if you want to retrieve the value of a single cell.
There is no need to set your sheet to active in order to change its name.
You can rewrite your code as following:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var currentCell = event.range;
var value = event.value;
if (value == "YES")
{
...
var sourceRow = range.getRowIndex();
var tabName = ss.getRange(sourceRow, 1).getValue();
...
ss.getSheetByName('Copy of CCTemplate').setName("CC" + tabName);
}
}
I have been trying to conditionally hide or filter a column if a range is empty. I guess the function "if" does not work since the column is hidden even when the range is not empty.
I will thank if someone check my code.
function OcultarSi() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var range = sh.getRange('E2:E24');
if (range.isBlank()){
spreadsheet.getRange('E:E').activate();
spreadsheet.getActiveSheet().hideColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
}
};
Below the code working for any user who needs a similar one. . .
function OcultarSi(){
var spreadsheet = SpreadsheetApp.getActive();
var range = spreadsheet.getRange('E2:E24');
if (range.isBlank()){
spreadsheet.getRange('E:E').activate();
spreadsheet.getActiveSheet().hideColumns(spreadsheet.getActiveRange().getColumn(),spreadsheet.getActiveRange().getNumColumns());
}
};
At the moment this is the function I'm using but I have no way of testing if it will work in the spreadsheet until I publish the application.
function readSelection() {
//The commented lines aren't needed if the sheet is open already
//var sheetid = "sheet id here";
//var spreadsheet = SpreadsheetApp.openById(sheetid);
//SpreadsheetApp.setActiveSpreadsheet(spreadsheet);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
//sheet.setActiveSelection("B2:B22");
var activerange = sheet.getActiveRange();
var activecells = activerange.getValues();
return activecells;
};
I assume you mean highlighted == selected
The result depends on whether the cells are contiguous or not (non contiguous cell selection is available in the new spreadsheets features http://googleblog.blogspot.co.nz/2013/12/new-google-sheets-faster-more-powerful.html
For contiguous cells selected your code returns the values of the selection as an array, for non-contiguous cells your code will return the an array with the single value of the LAST selected item.
I suggest that this is a bug in the implementation of the new spreadsheet. If it is important to you, I suggest you raise an issue. For the old spreadsheets, you can only select contiguous cells (eg B2:B22) so it will work as you expect.
The easiest way to answer this Q is to run the code you have written! You don't have to publish anything just run the code in the script editor of the spreadsheet you are examining
and look at the log.
function readSelection() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var activerange = sheet.getActiveRange();
var activecells = activerange.getValues();
Logger.log(activecells)
return
};
There is no way to do this at the moment or to obtain the selected ranges from a script.
A request is pending and you can support it here : https://code.google.com/p/google-apps-script-issues/issues/detail?id=4056
by adding a star to the request.
If/when this function is implemented your code would look as follows:
function readSelection() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var activeranges = sheet.getSelectedRanges();
var activecells = [] ;'
for (var ar in activeranges)
activecells = activecells.concat(activeranges[ar].getValues()) ;
Logger.log(activecells)
return ;
}
note that selected ranges may overlap, so some cell contents could be added twice.