No Longer Able to Access Spreadsheet Via URL - google-apps-script

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

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.

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

ClearSheet function not working on all tabs of spreadsheet

I have a Google sheet that needs certain cells to be cleared at midnight every night. It has 3 tabs titled 'DOV', 'EMD', and 'Taylor's sheet', and on each of these sheets the same range needs cleared (D6:D100).
I used 'Taylor's sheet' as a test sheet because I haven't coded in forever and wanted to to make sure everything worked well first. So I implemented the clearContent() for the designated range and everything came out fine. However, once I tried adding the 'DOV' and 'EMD' sheets like my original test sheet I keep getting errors regarding the getRange function.
function ClearSheet1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Taylor's Sheet");
sheet.getRange("D6:D100").clearContent();
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss1.getSheetByName("EMD");
sheet1.getRange("D6:D100").clearContent();
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = ss2.getSheetByName("DOV");
sheet2.getRange("D6:D100").clearContent();
}
I'm confused why using getRange is returning an error
"Cannot call method 'getRange' of null."
Do I need to break each of these out into its own function?

How do you make a google script run on whichever google sheet is open?

I made an app to insert data into a google spreadsheet. Usually you set the url of the google spreadsheet that the script is going to perform the actions specified in it.
function doGet(e){
var ss = SpreadsheetApp.openByUrl("your google spread sheet url");
var sheet = ss.getSheetByName("Sheet1");
Instead of setting a permanent url, i wanted the google script to perform the actions on a google spreadsheet that is open at that moment. This way if i create a new google spreadsheet, i don't have to change the url in the script
to add data since the app script will run on whichever spreadsheet is open.
Initially i tried this but it didn't work
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
When i used getActiveSpreadSheet and deployed it as a web app(testing the code) it gave me this error:
TypeError: Cannot call method "getSheetByName" of null. (line 4, file "Code")
Edited.
I tried this but wont work.
function doGet(e){
var newUrl = e.parameter.newurl
var ss = SpreadsheetApp.openByUrl([newurl]);
var sheet = ss.getSheetByName("Sheet1");

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.