Copy value from specific cell in google spreadsheet to another via button - google-apps-script

On the click on a Button I want a function to be triggered that copies the value from Sheet1!A6 to Sheet2!A6. (see template) !
What do I have to add to this to make it work?
function CopyPasteA6() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('A6').value
???
}
Thanks for help!

try like this, it's probably the simplest way to do it.
function CopyPasteA6() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
sheet1.getRange('A6').copyTo(sheet2.getRange('A6'))
}
then insert a drawing or an image in your sheet and assign the function to this image.
EDIT following your comment : this script copies to the next sheet if it exist. you'll have to put a button on each sheet though.... I'd suggest to use a menu instead that will be acessible from anywhere in the spreadsheet.
Note that 'A6' or "A6" are just the same ;-)
function CopyPasteA6() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheetidx = sheet.getIndex()-1 ;
var nextSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheetidx+1];
sheet.getRange('A6').copyTo(nextSheet.getRange('A6'))
}

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

How come this function isn't returning anything in Google Sheets?

I'm trying to be able to access information on the active sheet, but I can't even seem to "connect" with the sheet to begin with. I'm just wondering why this small function isn't returning anything.
I've played around with different ways to call the sheet (e.g. getActive(), getActiveSheet(), getSheetByName(), etc.), and changing the name of the sheet itself.
function CLICK (){
var sheetName = SpreadsheetApp.getActiveSheet();
return sheetName;
}
I just want it to return the sheet name so I know it's accessing the sheet properly.
For your var please try:
sheetName = SpreadsheetApp.getActiveSheet().getName();
Hi there are some different options you can choose depending on what you want to do. I would suggest to read https://developers.google.com/apps-script/guides/sheets. Here are the examples I have for you.
function myFunction() {
/* To work on the first sheet sheet of the Spreadsheet, use getName(); to get the name of the spreadsheet*/
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
/* To get the name of the active sheet*/
var sheet2 = SpreadsheetApp.getActiveSheet().getName();
/* To add values on the active cell (current selected cell)*/
var sheet3 = SpreadsheetApp.getActiveRange().setValue(sheet2);
/*To work on the sheet with a specific Name*/
var sheet4 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
console.log(sheet2);
console.log(sheet4);
/*The difference between getSheets()[0] and getSheetByName("name") is that getSheets()[0]
will always work with the first sheet even if you change position and getSheetByName() will
always work with the sheet you named even if you change position, the position is 0 based*/
}
Here is another function to get name, url and id of the srpeadsheet.
function getDetails(){
var getName = SpreadsheetApp.getActiveSpreadsheet().getName();
var getUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var getId = SpreadsheetApp.getActiveSpreadsheet().getId();
console.log(getName);
console.log(getUrl);
console.log(getId);
}
Remember that if you are using a script editor that is attached to the Spreadsheet for sure you will be working on that spreadsheet. Now if you created a new script directly from script.google.com then you will need to first open the spreadsheet by using an Id or a url to open that specific spreadsheet.
I hope this helps.

Rename sheet once copied to another Spreadsheet - remove "Copy of"?

Hopefully relatively simple, but can't seem to find an answer.
I have a function that copies (archives) the current sheet to another spreadsheet, but then it's called "Copy of [sheet name]" in the destination spreadsheet.
Hoping I can get rid of the Copy of part, but setName() hasn't seemed to work.
Everything in the below code works until the last two lines.
function archiveCurrentSheet() {
SpreadsheetApp.getUi()
var areyousure = Browser.msgBox('Are you sure you want to archive this sheet?', Browser.Buttons.OK_CANCEL);
if (areyousure == "cancel" ) {
}
else {
var source = SpreadsheetApp.getActiveSpreadsheet();
var currentSheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
var sheet = source.getActiveSheet();
var destination = SpreadsheetApp.openById("ID");
sheet.copyTo(destination);
var sheetRename = SpreadsheetApp.openById("ID").getSheets();
sheetRename.setName(currentSheetName);
}
}
It gives error: "TypeError: Cannot find function setName in object Sheet,Sheet,Sheet,Sheet,Sheet,Sheet,Sheet,Sheet."
Thanks!
p.s. bonus: would be how do I delete the current
The sheet.copyTo(destination) method returns the new sheet so you can do something like this.
sheet.copyTo(destination).setName(currentSheetName);

Google Apps Sheets Script .getRange Problems

I just want a "hello world" and after an embarrassing number of hours, its still not working. I'm new to Javascript and Google App script.
Here is the simple script that fails to enter the "Hello" and I would appreciate any tips you have on getting this to work.
As you can see in the commented lines, I've tried many ways to get my "Hello" out there, but none of them work yet. The menu does work. Thanks for your help
Really what I want to do is to be able to move from cell to cell in a spread sheet, get the values there, assign it to a variable. And then to write the var value to a cell after a condition is meet in the script.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Generate Coin Trades', functionName: 'GCTrades_'}
];
ss.addMenu('CoinTrade', menuItems)
function GCTrades_() {
var ss = SpreadsheetApp.getActive();
SpreadsheetApp.setActiveSheet(ss.getSheetByName('Prices'));
sheet.getRange("$H$1").setValue('Hello');
//settingsSheet.activate();
// One before the last row that has been entered in the spreadsheet
// var LastRow = sheet.spreadsheet.getLastRow() -1;
// var cell1 SpreadsheetApp.getActiveSheet().getRange(LastRow,16);
// var cell = spreadsheet.getRange("$H$1");
// cell.setvalue('Hello')
//var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// var cell = sheet.getRange(1655,16);
// var cell = 'H1655'
// cell.setValue("Hello");
//var count = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4)
// Spreadsheet.getActiveSheet().getRange(LastRow,16).setvalue('Hello')
// SpreadsheetApp.getActiveRange().setValue('hello')
}
There are a couple of typos preventing your script from running. First, your onOpen script isn't closed.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Generate Coin Trades', functionName: 'GCTrades_'}
];
ss.addMenu('CoinTrade', menuItems)
} // missing closing curly brace.
You used sheet.getRange()... but didn't define sheet anywhere. You also do not need to use setActive to edit the sheet, as you can do that on a cell-by-cell basis. It is enough to define the sheet in the script.
Once your sheet is defined, you can get ranges and set the values. I kept your absolute range ($H$1) for the first cell. We then use a relative reference for the second cell by getting the position using the .getRow() and .getColumn() methods.
function GCTrades_() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1')
var cell1 = sheet.getRange("$H$1").setValue('Hello');
var cell2 = sheet.getRange(cell1.getRow(), cell1.getColumn()+1).setValue('World');
}
Try this:
function onOpen()
{
SpreadsheetApp.getUi().createMenu('CoinTrade')
.addItem('Generate Coin Trades', 'GCTrades_')
.addToUi();
}
//This is a private function so it will not show up in script editors function list. If you want it to show up on the function list then remove the trailing underscore
function GCTrades_()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Prices').activate();//The activate is not required but it will force this sheet to be on top
sh.getRange("A1").setValue('Hello');//Do not use $s in the range here
}
What I really want:
Really what I want to do is to be able to move from cell to cell in a
spread sheet, get the values there, assign it to a variable. And then
to write the var value to a cell after a condition is meet in the
script.
It's not clear to me what you "really want". Perhaps, you can begin that code yourself and refine the description of what you "really want".

How to assign two functions to a single button

I only know how to assign a script to a button in Google Sheets but I don't know how to assign two or three function(scripts) to a single button. That when I press the button in making invoice the invoice change number, saves to next sheet, and print. How could it be.how can i run combine these three scripts at one in google sheets.
function export() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Unwanted Sheet');
sheet.hideSheet();
DriveApp.createFile(ss.getBlob());
sheet.showSheet();
}
function clearRange2() { //replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('A10:F26').clearContent();
}
function clearRange2() { //replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('A10:F26').clearContent();
}
}
Make another function that calls all the functions you need, then assign that function to the button.
Also if you are calling those functions from the same button, maybe those functions should all be combined into one function?