Find Value of a Cell- Google AppScript - google-apps-script

Trying to find value of Cell "B2" in Google AppScript so I can do something with it later. My code is not logging anything for this value. Any tips?
//Get Customer Name from Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cusRange = ss.getRange("B2:B2");
var cusName = cusRange.getDisplayValue();
Logger.log(cusName);
I've tried multiple modifications but cannot achieve my desired results.

I just ran your code like this and it works fine. I replaced ss with sheet in the third line. It worked for me.
function test()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cusRange = sheet.getRange("B2:B2");//replaced ss with sheet
var cusName = cusRange.getDisplayValue();
Logger.log(cusName);
}
I think you may have been viewing the execution transcript. Try logs instead. The selection right below it on the View Tab.

Related

No Longer Able to Access Spreadsheet Via URL

The below code used to work perfectly to get a range from one spreadsheet and use them as the values for a range in another spreadsheet. It will no longer work. I have tried openByUrl functions in other scripts and they will not execute, either. Error message that debugging gives me is that openByUrl cannot be found in object SpreadsheetApp. Any idea why?
function IMtasks(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var tss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/##############/edit#gid=#########');
var tasks = tss.getSheetByName("TASKS");
var tasksrange = tasks.getRange("A3:F102").getValues();
sheet.getRange("A202:F301").setValues(tasksrange);
}

Hide or filter rows or columns if specific sheets are empty in a google sheet macro

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());
}
};

Hide a Column function doesn't work

I am trying to hide a column with google spreadsheet, here is what I have tried:
var form = FormApp.getActiveForm();
var ss = SpreadsheetApp.openById(form.getDestinationId());
var sheet = ss.getSheets()[0];
sheet.hideColumn(8);
However, I get this error:
Cannot find method hideColumn(number). (line 50, file "Registration")
If I let IntelliSense do its work, it shows me that the method exists. What exactly am I doing wrong here?
Note: I would like to rull out any null references, because commands like the following work without a problem:
sheet.setColumnWidth(1, 130);
hideColumn excepts Range and not an integer value. If you want to pass a column number then use hideColumns. Hope this helps.
var form = FormApp.getActiveForm();
var ss = SpreadsheetApp.openById(form.getDestinationId());
var sheet = ss.getSheets()[0];
sheet.hideColumns(8);

how to switch to a variable sheet based on a cell value in google sheets

I am looking to create a button in google sheets and assign it a script which will take the user to a specific sheet based on a specific string in a cell.
I have played around trying to merge other answers to similar questions but i haven't been successful!
This is where i am at currently:
function sheetchanger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getRange("Lookup!B5");
ss.setActiveSheet(cell)
I am really, REALLY new to code, so please excuse the above if it is way off!
Thanks for any and all help :)
Try this:
function sheetChanger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Lookup");
var range = first.getRange("B5:B5");
range.activate();
}
Looking again, I think I misunderstood what you are trying to do. If you want to jump to Lookup!B5 then the above will work.
If you need to jump to the sheet that is named in Lookup!B5, then this will work:
function sheetChanger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Lookup");
var range = first.getRange("B5");
var cell = range.getCell(1,1);
var sheet = cell.getValue();
Logger.log(cell.getValue());
ss.getSheetByName(sheet).getRange("A1").activate();
}
It seems in my terrible knowledge of coding, I asked the script to look at the data in B5 but not to take it....
function sheetchanger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Lookup');
var cell = sheet.getRange("B5").getValue();
ss.setActiveSheet(ss.getSheetByName(cell));
}
I just needed to add the .getValue on it!

How could you retrieve a range of user highlighted cells in Google Sheets using Google Apps Script?

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.