Hide a Column function doesn't work - google-apps-script

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

Related

getSheetByName not working on setActiveSpreadsheet, but working on getActiveSpreadsheet in Google App Script

I have this code. It doesn't seem to work with setActiveSpreadsheet
var ss = SpreadsheetApp.openById('Id')
var booleanRange = SpreadsheetApp.setActiveSpreadsheet(ss).getSheetByName("Sheet1")
but works with getActiveSpreadsheet
var booleanRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
The error is
TypeError: Cannot read property 'getSheetByName' of null (line 4, file "Code")
setActiveSpreadsheet() sets the active spreadsheet but does not return that spreadsheet.
Instead, try
var ss = SpreadsheetApp.openById('Id')
var booleanRange = ss.getSheetByName("Sheet1")
SpreadsheetApp.setActiveSpreadsheet(ss) is a void function, meaning that it does not provide a result value to its caller (see image attached). Therefore you can not apply a method on it.
You can only run it like that and it will set spreadsheet ss as the active spreadsheet:
SpreadsheetApp.setActiveSpreadsheet(ss);
References:
setActiveSpreadsheet()

Google Apps Script to move cursor to last edit location is now failing

I've got some code that I've been using just fine for months and it suddenly fails. The code makes the google sheet go to the last edited cell upon opening.
function onEdit(e) {
var ss = e.source
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell();
var row = cell.getRow();
var column = cell.getColumn();
var sheet_name = sheet.getSheetName();
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('row',row);
scriptProperties.setProperty('column',column);
scriptProperties.setProperty('sheet name',sheet_name);
}
function onOpen(e) {
var properties = PropertiesService.getScriptProperties();
var ss = e.source;
var sheet_name = properties.getProperty('sheet name');
var sheet = ss.getSheetByName(sheet_name);
var row = properties.getProperty('row');
var column = properties.getProperty('column');
var cell = sheet.getRange(row,column);
ss.setActiveSheet(sheet);
ss.setActiveRange(cell);
}
When I look at the Failed Execution log I see this:
Feb 29, 2020, 7:43:12 AM Error Exception: Cannot convert '10.0' to int.
at onOpen(Code:26:18)
Has something changed on Google's back end that invalidates my code?
Problem
Passing String type to a method that expects Number as an argument.
Explanation
The error message is pretty self-explanatory: a String "10.0" cannot be converted to an Integer. Have you by any chance migrated to the new V8 runtime? The issue is easily confirmed there, this sample will result in the exact same error:
function testStringToIntError() {
const ss = SpreadsheetApp.openById('spreadsheet id');
const sh = ss.getSheetByName('4');
const store = PropertiesService.getScriptProperties();
const propName = 'testStrToInt';
store.setProperty(propName, '10.0');
const res = store.getProperty(propName);
sh.getRange(res,1);
Logger.log(res);
}
Solution
I would suggest making sure that row is of type Number by explicitly converting it with + or parseInt(), and that will work just fine.
Notes
Not sure why it worked for you before. UPD: indeed, after retesting the sample in Rhino, the runtime does allow type mismatch, so that's why the script worked before, but stopped now.
PropertiesService stores data as Strings, so you should always prepare it correctly when you use setProperty() or getProperty() (e.g. JSON.stringify() -> JSON.parse() for objects, etc).
Reference
getRange() method reference
Properties guide (anchored to data format section)

Google Sheets - Google app scripts - way to get full sheet data plus styling

Is there a way to get all sheet values, functions and stylings etc in one call to Google?
The only way I have found to do it is to call each style separately for the active range.
If possible i'd like to get all data/formulas/styling etc for the sheet in one go.
var sheet = SpreadsheetApp.getActiveSheet();
var activeRange = sheet.getActiveRange();
var backgrounds = activeRange.getBackgrounds();
var fontSizes = activeRange.getFontSizes();
var colours = activeRange.getFontColors();
var alignments = activeRange.getHorizontalAlignments();
var values = activeRange.getValues();
var formulas = activeRange.getFormulas();
var vAlignments = activeRange.getVerticalAlignments();
You can fetch full spreadsheet data using the Advanced Sheet Service as follows:
var ssID = "[YOUR_SPREADSHEET_ID]";
var fields = "developerMetadata,namedRanges,properties,sheets,spreadsheetId,spreadsheetUrl";
var fullSpreadsheetData = Sheets.Spreadsheets.get(ssId, {"fields":fields});
The call to Sheets.Spreadsheets.get(...) returns a Spreadsheet Resource object as defined by the Sheets API.
I'm not sure what your end goals are here, but you would always use the copyTo() method from the sheet class..
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var destination = SpreadsheetApp.openById('ID_GOES HERE');
sheet.copyTo(destination);
That will give you an exact copy of your sheet with all the formatting and data intact. The 'Sheet' object that you are returned when you call SpreadsheetApp.getActiveSheet() does contain all the data/formulas/styling, so in a way, the answer is yes. You can get it all with one call. Unfortunately, to use that data you have to use the individual methods contained in the 'Sheet' object that you already have listed. If it is just a copy of the sheet you need, I also dig the insertSheet() method which is part of the 'Spreadsheet' class. That would look like this..
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet(1, {template: templateSheet});
If neither one of those methods work for you, maybe including what your intentions are in the question will help the community find you an answer.

getLastRow() doesn't work correctly in my script

Ok so this doesn't work:
var range = mainList.getRange(1, 1, 50, 5).getValues();
var numRows = range.getLastRow()-1;
But this does
var numRows = mainList.getLastRow()-1;
range is CLEARLY a range? So why I am I getting a typeError?
TypeError: Cannot find function getLastRow in object DATA DATA DATA DATA DATA
In this situation we can tell that the variable range is actually a 2D array of data from the error, so there is no method getLastRow(). There is a range.length however.
var range = mainList.getRange(1, 1, 50, 5).getValues();
var numRows = range.getLastRow()-1;
It's actually kind of hard to see the value of your second example since we can't tell where mainList came from. Take a look at mcve
If mainList is a spreadsheet then we can go to the spreadsheet documentation here and in the example here we see that we can define the range in A1Notation by specifying the name of the sheet( sometimes called tab) as so:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");
Personally I usually like to take the time to do something like this:
var spreadsheet = SpreadsheetApp.getActive(); // or SpreadsheetApp.openById('ssid'); I Google Drive the ids are the primary key to locating files not names. And you can get the id of a spreadsheet right of of the url whenever you open it up.
var sheet = spreadsheet.getSheetByName('sheetname');
var range = sheet.getRange(1,1,50,5);
var valuesArray = range.getValues();
Then you can loop through values array as shown below:
for(var i=0;i<valuesArray.length;i++){
for(var j=0;j<valuesArray[i].length;j++){
var cellvalue=valuesArray[i][j];
}
}

Find Value of a Cell- Google AppScript

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.