Easy way
Use the built-in IMPORTRANGE()
function in Google Apps:
In your Target cell, type the following formula
=IMPORTRANGE("FILE_ID_HERE","Sum all!G10:G10")
The syntax for this function is
=IMPORTRANGE("FILE_ID","SHEET_NAME!RANGE_START:RANGE_END")
When you first type in this function, you'll get an error in the cell. Simply click on it and select "Allow" to link the two sheets together. This error will occur even if it is the same spreadsheet. This function can link two separate spreadsheets, too, as long as you have edit access to both.
Hard Way
I'm assuming from your question that you want to copy values to and from the same spreadsheet document, but to different cells that are located on different sheets of the spreadsheet. The Google Apps Script API calls tabs "sheets" and the overall document "spreadsheet".
First, open the script editor
- Open your spreadsheet that you'd like to make this script for.
- Select "Tools" in the toolbar, then "Script Editor"
Second, make a function for onEdit
.
Making a function named onEdit
will create a function that runs every time the edit
trigger is fired, using a no-authorization "simple trigger". Google Sheets automatically sends this event every time a cell is edited by a user. The argument e
for the function is the event passed by the trigger.
function onEdit(e) {
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sum all");
var cell = source .getActiveCell();
if (cell.getRow() == 10 && cell.getColumn() == 7) {
var target = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyData");
target.getRange("T12").setValue(cell.getValue());
}
}
Third, save the script
Save the script, reload the file, and test it out.
If your tabs are on different spreadsheets
Change this line:
var target = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyData");
to this:
var target = SpreadsheetApp.openById("FILE_ID").getSheetByName("CopyData");
and insert the file ID for the target spreadsheet where I've written FILE_ID
.
You will also need to use an "installed trigger", since a simple trigger cannot open a remote spreadsheet. To do this, change the name (so it is no longer a simple trigger function), and follow the steps here
onEdit(e)
function. Google Documentation – Alan WellsgetValue()
andsetValue()
method. But in order to use those methods they must be chained to a range. And in order to get a range, you need to get the sheet. And in order to get the sheet, you need to get the spreadsheet. Google documentation - getValues() – Alan Wells