Google Sheets function to rename sheet to equal cell value - function

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
}

Related

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

Paste Cell Reference from another worksheet

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
}

How to Ignore Empty Rows in getRange()getValues() with A1 notation

Teaching myself how to code in Google Apps Script and I can't figure out how to ignore blank rows in the array, based on getRange A1 notation.
This other post works if my range is only one column, but I'm trying to do it if a whole row is blank, not just the value in one column.
How to ignore empty cell values for getRange().getValues()
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sample");
var data = sheet.getRange("A1:E").getValues();
var filtered = data.filter(String);
Logger.log(filtered);
}
In the code sample above, my log still shows all of the blank rows after the populated rows. If I change the A1 notation to A1:A, then it works, but I want to filter based on the whole row.
Thanks for helping a newbie.
Try this:
function myFunction() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("sample");
var data = sheet.getRange(1,1,sheet.getLastRow(),5).getValues();
var filtered = data.filter(String);
Logger.log(filtered);
}
You could do it this way too.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
var data = sheet.getRange("A1:F").getValues();
var filtered = data.filter(function(r){
return r.join("").length>0;
});
Logger.log(filtered);
}

Adjust google sheets script to retrieve text from all rows to add notes to cell

I'm trying to adjust this script in google sheets. The goal is to retrieve the text from a cell column B and add this as a comment to the cell in column A.
The script that is hardcoded for a specific cell (A2 + B2)
`function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Test');
var targetCell = sheet.getRange("A2");
var sourceCell = sheet.getRange("B2");
var noteText = sourceCell.getValue();
targetCell.setNote(noteText);
}`
How do I change this into a dynamic script that retrieves the text in every row in column B and adds it to the comment of every cell of column A?
For those who is looking for a final solution:
function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange("A2:B5");
range.clearNote();
for (var x=1;x<5;x++) {
range.getCell(x, 1).setNote(range.getCell(x, 2).getValue());
}
}

Copying values from one sheet to another

I'm a new user of the Google Sheets and could use some help.
I have values in range A2:G2 on my currently active sheet. When the user clicks a button, I want those entered values to be copied into a 2nd sheet ('Sheet4') in the same main spreadsheet on the next open row.
I thought this would work, but the script is not finding the last row or adding the temptext values.
function button1(){
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValue();
var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();
activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));
var lastrow = activesheet2.getLastRow(1);
activesheet2.getRange(lastrow + 1,1).setValue(temptext);
}
You need to get and set multiple values, and take care with the parameters to the functions (some take none).
function button1(){
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValues();
var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();
activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));
var lastrow = activesheet2.getLastRow();
activesheet2.getActiveSheet().getRange(lastrow+1,1,1,7).setValues(temptext);
}