copyInfo from one sheet to another sheet in google sheet with script - google-apps-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.

Related

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

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

After Three Days of Experimenting and Searching, I STILL Can't Get .getValue Function to Work

Sorry for the long subject line, but this site wouldn't accept a more concise statement.
I have a Google Sheet, that has nothing more than my name in Cell A1.
Then I went to Tools / Script Editor, and have a "Code.gs" script that says:
function onEdit(e) {
Logger.log("We're in the function.");
var ss = Spreadsheet.App.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var valueOfCell = sheet.getRange(1,1).getDisplayValue();
Logger.log("Value of Cell is '" + valueOfCell + "'.");
}
When I edit my very simple sheet, and check the log (View / Logs), I get:
[19-02-28 08:58:10:182 CST] We're in the function.
That's it. I've tried every permutation I can think of or suss from three days of web-searching, and I simply can not make the .getValue() (or .getDisplayValue()) work.
What am I doing wrong?
Thanks!
/Kent
"Spreadsheet.App" should be "SpreadsheetApp".
Additional permutations that work:
Logger.log("We're in the function.");
var ss = SpreadsheetApp.getActiveSheet();
var valueOfCell = ss.getRange(1,1).getDisplayValue;
and
Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1,2).getDisplayValue();
and
Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSheet().getRange(1,2).getDisplayValue();
After four days, I think I finally understand some of this. For others as dense as I, I leave this documentation here:
Spreadsheet is the "workbook", the collection of Sheet tabs
Sheet is the individual tab'd sheet in a Spreadsheet "workbook".
Seems like simple enough information, but I couldn't find this documented anywhere.
SpreadsheetApp is the Class, the "parent" of all the Spreadsheets. While probably not technically accurate, for purposes of my thinking, I think of "SpreadsheetApp" as being the "Sheets" application within Google Docs, as opposed to "Docs" or "Slides".
So,
var ThisApp = SpreadsheetApp;
// Sets the variable "ThisApp" to "SpreadsheetApp", which I guess is the name/identification of the spreadsheet app.
var TheActiveWorkBook = SpreadsheetApp.getActive();
// Sets "TheActiveWorkBook" to the identifier for the current collection of sheets (collection of tabs within a spreadsheet).
var TheActiveWorkBook = SpreadsheetApp.getActiveSpreadsheet();
// Identical to above; just a different name for it.
var ActiveWorkBookName = SpreadsheetApp.getActive().getName();
// Sets the variable to the *name* of the workbook/spreadsheet (like, "Fred's Spreadsheet").
var ActiveSheetInWB = SpreadsheetApp.getActiveSpreadsheet();getActiveSheet();
// Sets the variable to the identifier of the active tab in the spreadsheet.
var ActiveSheetInWB = SpreadsheetApp.getActive();getActiveSheet();
// Identical to above.
var ActiveSheetInWB = SpreadsheetApp.getActiveSheet();
// Identical to above. The active sheet is the active sheet, regardless of whether we know the active spreadsheet, so the extra step of including the active spreadsheet, as we did in the previous two examples, is unnecessary. (The system knows that the active spreadsheet is the one that contains the active sheet.)
var ActiveSheetInWB = SpreadsheetApp.getActiveSheet().getName();
// Gets the *name* of the sheet, as it shows on that sheet's tab.
var sheet = SpreadsheetApp.getActiveSheet();
var ActiveSheetInWB = sheet.getName();
// Shortens the long "SpreadsheetApp.getActiveSheet()" to a short reference to save typing in subsequent uses. Otherwise identical to above example.
I think I'm finally getting the hang of this....

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?

Copying formats only from separate Google Sheet

I'm trying to copy formatting only from a source sheet that is in a different workbook than the current sheet. Essentially, the source sheet format changes and I'd like to pull updates to the formatting into my destination sheet (which is the current sheet I would be working in). I'm using the following code, but I get "Cannot convert Spreadsheet to (class)" error" on the last line. Any insights?
var ss = SpreadsheetApp.openById("Source ID")
var source = ss
var destination = SpreadsheetApp.openById("Destination ID");
var range = source.getRange("N3:AE50");
range.copyFormatToRange(destination, 10, 27, 4, 50);
You want to copy the format from Spreadsheet A to Spreadsheet B. If my understanding is correct, how about this workaround? I think that there are several answers for your situation, so please think of this as one of them.
As tehhowch pointed out, copyFormatToRange() can be used for the same sheets in a Spreadsheet. For this situation, I understood that you want to use for between 2 Spreadsheets. In my workaround, I used the following flow.
Copy the source sheet of Spreadsheet with "Source ID" to Spreadsheet with "Destination ID".
Retrieve the range from the copied sheet as the source range.
Copy the source range to the destination sheet using copyFormatToRange().
Delete the copied sheet.
Sample script :
var sourceA1Notation = "N3:AE50";
var destinationA1Notation = "D10"; // row=4, col=10 This is the cell of upper left for putting the source range.
var source = SpreadsheetApp.openById("Source ID");
var destination = SpreadsheetApp.openById("Destination ID");
var sourceSheet = source.getSheets()[0]; // You can also use source.getSheetByName(name)
var destinationSheet = destination.getSheets()[0]; // You can also use source.getSheetByName(name)
var copiedsheet = sourceSheet.copyTo(destination);
var sourceRange = copiedsheet.getRange(sourceA1Notation);
var destinationRange = destinationSheet.getRange(destinationA1Notation);
sourceRange.copyFormatToRange(
destinationSheet,
destinationRange.getColumn(),
destinationRange.getColumn() + sourceRange.getNumColumns() - 1,
destinationRange.getRow(),
destinationRange.getRow() + sourceRange.getNumRows() - 1
);
destination.deleteSheet(copiedsheet);
Note :
When you use this, please input and confirm sourceA1Notation, destinationA1Notation, "Source ID" and "Destination ID".
destinationA1Notation is the cell of upper left for putting the source range.
In this sample script, each first sheet of "Source ID" and "Destination ID" is used. If you want to use other sheet, please modify this script.
References :
copyFormatToRange()
getSheets()
getSheetByName()
If I misunderstand what you want, I'm sorry.