Exception: You do not have permission to call hideColumns (line 25) - google-apps-script

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.

Related

copyInfo from one sheet to another sheet in google sheet with script

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.

GoogleSheets ApsScript ReferenceError: ADDRESS is not definedDetails

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

Google script, how to access and paste on edit to a specific sheet within another spreadsheet?

I am attempting to create a script so that when a change is made in one spreadsheet the corresponding sheet with the same name in another spreadsheet receives the same change in the same place, and so I can put one lot of the script in each one and get two linked sheets that affect each other.
this is the code:
var targetID = 'ID of the sheet';
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
function onEdit(e){
var range = e.range;
var value = e.value;
var row = range.getRowIndex();
var column = range.getColumnIndex();
exportValue(row,column,value)
}
function exportValue(row,column,value) {
//(**this is the point where it breaks**)
var s = SpreadsheetApp.openById(targetID).getSheetByName(targetSheet);
var target = s.getRange(row, column);
target.setValue(value);
}
In my version i put in logs in between each line to see where it failed and it got to the line where i have put "this is the point where it breaks" and then didn't return anything after that.
Since attempting this I tried to open up the other file by just pulling out all of the variables but I couldn't get it to work.
the current error messages that it is going with are:
Cannot find function getSheetByName in object Sheet
Cannot find function openById in object Spreadsheet
I have spent so much time on this already and I feel like the answer is really simple but I would really appreciate any advice
Thanks in advance
:)
Linked Spreadsheets with Installable onEdit() triggers
I got this to work by using an installable onEdit(e) Trigger.
function Linked1Edit(e){
var ss=SpreadsheetApp.openById('The Other Spreadsheets ID');
var sh=ss.getSheetByName(e.range.getSheet().getName());
var rg=sh.getRange(e.range.rowStart,e.range.columnStart);
rg.setValue(e.value);
}
I called one Linked1Edit(e) and the other Linked2Edit(e) and created an installable onEdit(e) trigger for each one and now they write to each other.
Unfortunately, it only works for single valued changes though.
The following script will allow you to make changes to more than one value at a time.
function Linked1Edit(e){
var ss=SpreadsheetApp.openById('The Other Spreadsheet ID');
var sh=ss.getSheetByName(e.range.getSheet().getName());
var rg=sh.getRange(e.range.rowStart,e.range.columnStart,e.range.rowEnd-e.range.rowStart+1,e.range.columnEnd-e.range.columnStart+1);
var vA=e.range.getValues();
rg.setValues(vA);
}
Event Object Link
Creating a Trigger
Select Current Project's Triggers
Click Add Trigger:
Create Trigger Dialog:
You can also create a trigger in code. See ScriptApp Class

When using setFormulas I get error Cannot find function error "object function getFontColor() {/* */}."

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

Script gives error when spreadsheet range doesn't exist

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