I have this simple line of code in my Google Apps Script:
s.getRange('C2:C').clearContent();
but sometimes my spreadsheet only has one row, so C1 is there but C2:C does not exist. In such case, the script gives an error.
Does anyone know how I can achieve the same function as the above code but so that there is no error when only Row 1 exists?
I have this so far, but I don't know if there's anything wrong with it. It seems too simple:
function Test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet1');
var values = s.getRange('C:C');
var howManyRows = values.getNumRows();
if (howManyRows >= 2)
s.getRange('C2:C').clearContent();
}
}
the method getMaxRows() returns the number of available rows in a sheet, so you could use it in a condition like this :
if(s.getMaxRows()>1){s.getRange('C2:C').clearContent()}
Related
I want to copy some data from sheet 1 to sheet 2, but every time I tried to run the below-attached code it send me an error which I mention in the comment.
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet 1");
var pasteSheet = ss.getSheetByName("Sheet 2");
// get source range
var source = copySheet.getRange(2,1,38,17);
// get destination range, "TypeError: Cannot call method "getRange" of null."
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,38,17);
// copy values to destination range
source.copyTo(destination,{contentsOnly: true});
}
Thanks in advance.
Your code seems correct and is working for me,
Can you try to run this code and check if its logging the correct name of your google sheet that you are using,
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getName());
}
For better access to a particular google sheet you can use the following method to access the file rather than using getActiveSpreadsheet
var ss = SpreadsheetApp.openById("your sheet id");
You can even try the debug window to check what is the present values of different variables and get some idea of possible errors.
The error you are receiving is due to the fact that pasteSheet is null.
This is most likely due to one of the following reasons:
your spreadsheet does not have any sheet named Sheet 2 - I suggest you double check the name of the sheets from your spreadsheet;
the active spreadsheet you are retrieving is not the one you are needing for copying the data;
As suggested above, you can indeed make use of the openById method in order to retrieve the needed spreadsheet and afterwards get the actual sheets.
Reference
openById(id);
Apps Script Troubleshooting.
When I try and run the following functions I receive the error: "Exception: You do not have permission to call showColumns or hideColumns dependnig on which function I run (line 25)." Any ideas how to resolve this?
function showVTTAColumns() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var VTTASheet = sheet.getSheetByName("LiveResults");
VTTASheet.showColumns(17); // Column Q
}
function hideVTTAColumns() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var VTTASheet = sheet.getSheetByName("LiveResults")
VTTASheet.hideColumns(17); // Column Q
}
getActiveSheet() doesn't take any arguments. It just returns the sheet that is currently active in the spreadsheet. Instead try
function showVTTAColumns() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var VTTASheet = sheet.getSheetByName("LiveResults");
VTTASheet.showColumns(17); // Column Q
}
and see if that works?
Notes:
I don't know if the exclamation mark is included in your sheet name (the name of the tab). If it is, please add it after the name.
Also check if the column is protected. If it is, make sure you have permissions to edit.
References
getActiveSheet
getSheetByName
UPDATE
It seems that you are trying to use the script as a custom function. Note that not every google script is a custom function. A custom function must have a return value and can not affect cells other than those it returns a value to. Hiding and showing columns with a custom function is simply not possible.
See this link for more information on custom functions.
I'd suggest you create a function that can be started from a menu or from a button. This function can check the contents of C19 and then starts the appropriate function.
function showVTTAColumns() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var checkCell = sheet.getSheetByName('.....').getRange('C19').getValue();
checkCell === "VTTA" ? showVTTAColumns() : hideVTTAColumns();
}
I don't know why or how but the following code seemed to do what I wanted and works:
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var resultsSheet = sheet.getSheetByName("LiveResults");
resultsSheet.hideColumns(30);
It's no different to my original code so I'm very confused as to why it suddenly worked without any errors.
I know this has been asked before but none of the answers seem to be working for me. I need to get the values of cells that are clicked on. As a very basic prototype, I created a script tied to my spreadsheet with the following:
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getActiveCell().getValue());
}
I then go into my spreadsheet and select a cell with a value and run the function in the script window. No value is displayed in the log.
Next I tried:
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}
I then go into my spreadsheet and select a cell with a value (G6) and run the function in the script window. The log displays 1,1.
And finally I tried
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Browser.msgBox(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}
I then go into my spreadsheet and select a cell with a value (G6) and make a change to it. When I hit enter, the popup dialog displays 1,1.
This seems like it should be really simple but for the life of me cannot figure it out. Can anyone help?
I tested this in a spreadsheet and I found that using var sheet = ss.getActiveSheet(); yielded the correct results whereas using the .getSheets()[] method did not.
This is a bit late, but I was researching this today, and wanted to share: My issue was similar, in that sheet.getActiveRange().getRow(), sheet.getActiveCell().getRow(), etc., all returned row 1, regardless of what cell or range was active.
In this case, I was opening the script from a previous bookmark I created. Opening the script from a bookmark apparently doesn't re-establish the active relationship with the sheet.
Once I opened the script from the spreadsheet instead, my code returned the correct row reference.
I have tried following code and works fine. getRow() method is of Range class not Sheet class.
var ActSpSheet = SpreadsheetApp.getActiveSpreadsheet();
var ActSheet = ActSpSheet.getActiveSheet();
var GetCell = ActSheet.getCurrentCell();
var GetCurRow = GetCell.getRow();
Logger.log(GetCurRow);
I'm trying to create a script that copies down formula the length of the sheet (stitched together on the back of other SO posts I found while researching):
var originRange = ss.getSheetByName('data_prep').getRange('C6:I6');
var formulas = originRange.getFormulas();
var target = ss.getSheetByName('data_prep').getRange('C7:I');
for(i in target) {
target[i].setFormulas(formulas);
}
When I run this function I get:
TypeError: Cannot find function setFormulas in object function getFontColor() {/* */}.Dismiss
What I had hoped/expected was for all the formulas in C6:I6 to be copied into each row starting C7 down the length of the sheet.
The function works for one row at a time. So if I set target as just C7:I7 and get rid of the for loop it works.
Why am I getting this getFont error?
How can I get my script to copy the formulas in C6:I6 down the entire sheet?
As an alternative to 2, how can I get my script to copy down as far as while there is data in column A?
Maybe try something like this:
var ss= SpreadsheetApp.getActiveSpreadsheet();
var dataPrepSheet = ss.getSheetByName('data_prep')
var originRange = dataPrepSheet.getRange('C6:I6');
var everyRow = dataPrepSheet.getMaxRows().toString();
var target = dataPrepSheet.getRange('C7:I' + everyRow);
originRange.copyTo(target);
SpreadsheetApp.flush();
Use the getMaxRows() method to get every every row in the spreadsheet, whether it's got content or not. That returns an integer, so convert it to a string then use a text formula to create the A1 notation.
Then, don't set the formulas, use copyTo()
Hi im having trouble passing 2 cells in my scripts parameters:
function myFunction(c1, c2) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]; //This is the correct sheet
var valueFirstCell = sheet.getRange(c1).getValue();
var valueSecondCell = sheet.getRange(c2).getValue();
return valueSecondCell - valueFirstCell;
}
So i use this in cell A3:
=myFunction(A1, A2)
And lets say A1=10 and A2=15 i would like A3 to show 5 but,
var valueFirstCell = sheet.getRange(c1).getValue();
fails "Argument has to be a range"
Any ideas?
In the custom function, c1 and c2 in your script are actually going to be the value of what's in those cells, so you can just use them directly. You can see that by adding Logger.log(c1); and Logger.log(c2); to your script. Try this:
function myFunction(c1, c2) {
Logger.log(c1);
Logger.log(c2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]; //This is the correct sheet
return c2 - c1;
}
There's a tutorial here on building a simple custom function that shows a similar example.
It's juste saying that argument of getRange function is not in the way it's expected.
link to getRange() documentation
getRange() function expects row and column.
For e.g. valueFirstCell is in 7th row and 1st column.
var valueFirstCell = sheet.getRange(7,1).getValue();
Link to getRange() official documentation