Paste Cell Reference from another worksheet - google-apps-script

I am trying to get this script to work but cannot work it out.
So i would like the result on Sheet 1 from Sheet 4.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
ss.getRange('AM2').activate();
ss.getCurrentCell().setFormula('=[4] AL3');
I hope i am close ?

Explanation:
Firstly, define the sheets you want to work with:
var sheet1 = ss.getSheetByName('Sheet1');
var sheet4 = ss.getSheetByName('Sheet4');
Get the value of cell AL3 in Sheet4:
var value = sheet4.getRange('AL3').getValue();
Set it to cell AM2 in Sheet1:
sheet1.getRange('AM2').setValue(value);
If you want to use the formula instead, then just do that:
var sheet1 = ss.getSheetByName('Sheet1');
sheet1.getRange('AM2').setFormula('Sheet4!AL3');
Solution:
I think you are looking for this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet4 = ss.getSheetByName('Sheet4');
var value = sheet4.getRange('AL3').getValue();
// sheet1.getRange('AM2').setValue(value); // if you want the value use this
sheet1.getRange('AM2').setFormula('Sheet4!AL3'); // if you want the formula use this
}
If the name of the sheets have a space between the word and the number, for example Sheet 1 instead of Sheet1 and Sheet 4 instead of Sheet4, then use this solution:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet 1');
var sheet4 = ss.getSheetByName('Sheet 4');
var value = sheet4.getRange('AL3').getValue();
//sheet1.getRange('AM2').setValue(value); // if you want the value use this
sheet1.getRange('AM2').setFormula("'Sheet 4'!AL3") // if you want the formula use this
}

Related

Add value offset 1 from active cell together in spreadsheet script?

I want to to add value of cell B1 onto the existing value of A1. I would like the script to be relative, and leave the A1 cell as a pure value. Bonus if cell B1 is cleared at the end of the script.
function myFunction() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var CurrentCell = cellRange.getValue ();
var RightCell = getvalue.offset(0, 1);
Logger.log(`selectedColumn: ${selectedColumn}`);
Logger.log(`selectedRow: ${selectedRow}`);
Logger.log(`selected cell vale: ${cellRange.getValue()}`);
Logger.log(`selected cell vale: ${cellRange.getValue(0, 1)}`);
cellRange.setValue(CurrentCell+RightCell);
It leaves with me with the value of A1 plus 'Range'. If A1 is 7 it returns (7Range).
Trying to do this in Google App Scripts
Add value of cell one column to the right to current cell.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r.getValue());
}
I get the same thing you do when I replace this with:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r);
}
And I'm kind of surprised it doesn't return an error. But yeah it returns the class name

Copy dynamic range from one sheet to another

I am using the below code to copy my data from sheet1 to sheet2 and append the data in sheet2 if script runs again? The issue is that my range is not fixed in sheet 1,I want to copy whatever range contains the content should be copied to sheet2 Can anyone guide that what to change in the below code to get the result?
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
var source = copySheet.getrange(2,1,200,7);
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,200,7);
source.copyTo(destination, {contentsOnly:true});
var data = copySheet.getRange(2,1,200,6)
data.clearContent()
}
Method1:
Include:
var paste_lr = pasteSheet.getLastRow()
Modify:
var source = copySheet.getrange(paste_lr+1,1,200,7);
Similarly change 200 using same function.
Method2:
var sp = PropertiesService.getScriptProperties();
var SourceLr = sp.getProperty("Source_Data_Rows") || 0;
var source = copySheet.getrange(SourceLr+1,1,200,7);
var Source_Lr = copySheet.getLastRow();
sp.setProperty("Source_Data_Rows", Source_Lr);

Is it possible to insert cells into another sheet in Google Sheets through script

I have a Google-Sheet script that creates few cells and inserts them into active sheet. I would like to ask someone for help. I would need the script to do exactly same work, but only change I would need is to pick the sheet where the cells will be added. At this moment it always applies to the active sheet.
As you may recognized I am absolutely noob in coding and also in English, so I really appriciate everyone who tries to help me with this.
Thank you very much, Petr
The code I have right now:
function myButton(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
How about the following modification patterns?
Pattern 1:
When you want to select the specific sheet in the active Spreadsheet, you can modify your script as follows.
function myButton() {
var sheetName = "Sheet2"; // Added. Please set the sheet name you want to select.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName); // Modified
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
Pattern 2:
When you want to select the specific sheet in the other Spreadsheet from the active Spreadsheet, you can modify your script as follows.
function myButton() {
var spreadsheetId = "###"; // Added. Please set the spreadsheet ID you want to use.
var sheetName = "Sheet2"; // Added. Please set the sheet name you want to select.
var ss = SpreadsheetApp.openById(spreadsheetId); // Modified
var sheet = ss.getSheetByName(sheetName); // Modified
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
Note:
In this answer, from I would need the script to do exactly same work, but only change I would need is to pick the sheet where the cells will be added. At this moment it always applies to the active sheet., it supposes that your script works fine for the 1st tab in the active Spreadsheet. Please be careful this.
When getSheets() is used, for example, in the case of var sheet = ss.getSheets()[0];, it's the 1st tab. In the case of var sheet = ss.getSheets()[1];, it's the 2nd tab.
References:
getSheetByName(name)
openById(id)
getSheets()

Google Sheets function to rename sheet to equal cell value

I'm trying to get my sheet names to equal cell values, whether it be text or numbers, but I've come up short.
After numerous google searches, and testing, this is as close as I've gotten to an answer.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]
var cell = sheet.getRange("A2")
sheets[3].setName(cell);
}
You need to getValue¹ from the range you got.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]; //Sheet 2
var cell = sheet.getRange("A2");//Sheet2!A2
var value = cell.getValue();//Added
sheet.setName(value);//Sheet2 name changed
}

Past in the next empty row from specific range in the Google SpreadSheet

I want to past in the next available space from A:G.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet1");
var sheet2 = ss.getSheetByName("Sheet2");
sheet1.getRange("A1:G1").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1,1,7), {contentsOnly:true}); }
If my cell "I6" isn't empty i'd have this result:
Image with an example