Name new sheet according to cell value - google-apps-script

Trying to combine two scripts here:
1) Get a cell value in a specific sheet
function getval() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('master'), true);
spreadsheet.getCurrentCell().offset(2, 4).activate();
};
2) Create a new sheet and rename it according to cell value
spreadsheet.insertSheet(1);
spreadsheet.getActiveSheet().setName('');
The second step is where I struggle. How can I use the results of the getval function as a variable to place inside setName?

function insertAndNameNewSheet() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('master');
var name=sh.getCurrentCell().offset(2, 4).getValue();//2 rows down and 4 columns to the right
ss.insertSheet(name,1);
}
insertSheet(name,index)
Best Practices

Related

Add custom formulas to Google sheets using Apps script

I'm trying to calculate to cells but not working. This is my code :
function total() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var cell = sheet.getRange("A3");
var calculate = cell.setFormula("=SUM(A1+A2)");
return calculate
}
My cell is A1 AND A2 I want to put result in cell A3 by add "=total()".
not working but if run from script it work.
I want to put =total() in cell A3 and work without see my formula in sheet.
I wand to send copy of my sheet to people and he can read and edit it.
I don't need people to see my formulas.
or any another way to hide my formulas from people
You need to calculate the result in the script:
function total() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var values = sheet.getRange("A1:A2").getValues();
var value = values[0][0] + values[1][0];
return value;
}
Alternatively, you can
Calculate normally with formulas in another spreadsheet
Use IMPORTRANGE to import the data from that spreadsheet such that only values but not formulas are display in the cells
However, he cannot make changes to the range directly.
He need to first copy and paste by values.
Your custom formula is returning the return value of the function Range.setFormula(formula), which is a Range. And you want a number. For this you need the values of both cells "A1"and "A2".
Furthermore, if you are using this custom formula to set a value in cell "A3" and not a formula then you can just have the custom formula return the value.
Example:
function total() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var values = sheet.getRange("A1:A2").getValues();
var value = values[0][0] + values[1][0];
return value;
}

How to hyperlink an array of text using URLs in the sheet?

In my Spreadsheet I have a column of data with book titles, and a column data with of URLS in it, about 5000 rows. I would like to use appscript to make a new column of data with those same titles hyperlinked using the URLS in the other column.
I'm trying to do this with arrays and the map function but I'm not sure how to use the map function so that the titles become hyperlinked. I'm very new at this so need a bit of help. Here's what I'm doing so far, the "????" in the MakeURL function is placeholder for the fix. Any suggestions?
Thank you for helping!
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet=ss.getActiveSheet();
function mainfunction() {
//Get both non-contiguous values into separate arrays.
var titles = activeSheet.getRange('E:E').getValues();
var theURLS = activeSheet.getRange('Q:Q').getValues();
//Put both values into a single unified array
var unified=[];
for (var i=0;i<titles.length;i++){
unified.push(titles[i],theURLS[i])
}
//Run a transformation on the values of the unified array and output it into newTitles array
var newtitles = unified.map(makeURL);
//write the new hyperlinked titles into a range(column).
activeSheet.getRange('Z:Z').setValues(newtitles);
};
function makeURL(row){ return ?????; };
I believe your goal as follows.
You want to retrieve the values from the columns "E" and "Q".
The columns "E" and "Q" are the title and the hyperlink, respectively.
You want to create the hyperlink value using the title and hyperlink, and want to put the values to the column "Z".
You want to achieve this using Google Apps Script.
In this case, I would like to propose to use RichTextValues. When this is reflected to your script, please modify as follows.
Modified script:
When you run the function of the modified mainfunction, the hyperlink values created from the columns "E" and "Q" are put to the column "Z".
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
function mainfunction() {
//Get both non-contiguous values into separate arrays.
var lastRow = activeSheet.getLastRow();
var titles = activeSheet.getRange('E1:E' + lastRow).getValues();
var theURLS = activeSheet.getRange('Q1:Q' + lastRow).getValues();
var richTextValues = titles.map(([e], i) => [SpreadsheetApp.newRichTextValue().setText(e).setLinkUrl(theURLS[i][0]).build()]);
activeSheet.getRange('Z1:Z' + richTextValues.length).setRichTextValues(richTextValues);
}
Note:
If you will achieve your goal using the built-in formula, I thought that you can also use the following formula. In this case, please put it to the cell "Z1".
=ARRAYFORMULA(HYPERLINK(Q:Q,E:E))
If you want to put the formulas using Google Apps Script, I thought that you can also use the following script.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
function mainfunction() {
//Get both non-contiguous values into separate arrays.
var lastRow = activeSheet.getLastRow();
var titles = activeSheet.getRange('E1:E' + lastRow).getValues();
var theURLS = activeSheet.getRange('Q1:Q' + lastRow).getValues();
var formulas = titles.map(([e], i) => [`HYPERLINK("${theURLS[i][0]}","${e}")`]);
activeSheet.getRange('Z1:Z' + formulas.length).setFormulas(formulas);
}
References:
Class RichTextValueBuilder
setRichTextValues(values)

Adding a row to a named range

I have a named range in Google Sheets (A1:K14). All I'm trying to do is add a new row at the bottom of the named range. It seems like an easy task. The named range doesn't expand using this code and I don't get an error message. It does insert a new row outside of the named range, which is not what I'm trying to do. If I change to insertRowBefore (lastRow), the new blank row is inserted and the named range is expanded. I'm teaching myself GAS, so this is probably a simple mistake on my part
var sheet = SpreadsheetApp.getActiveSheet()
var Range = sheet.setActiveRange(range);
var data = Range.getValues();
Logger.log(range.getA1Notation());
var lastRow = SpreadsheetApp.getActiveSheet().getRange('DataRange').getNumRows();
SpreadsheetApp.getActive().insertRowAfter(lastRow);
return range;
Adding rows and/or columns to a namedRange
This function has the defaults setup to add one row.
//r is number of rows to add
//c is number of columns to add
function addRowAndColumnsToNamedRange(name,r,c) {
var name=name||'One';//default name
var r=r||1;//default number of rows to add
var c=c||0;//default number columns to add
var ss=SpreadsheetApp.getActive();
var nrgA=ss.getNamedRanges();
for(var i=0;i<nrgA.length;i++) {
if(nrgA[i].getName()==name) {
var nr=nrgA[i];
var h=nr.getRange().getHeight();
var w=nr.getRange().getWidth();
var sh=nr.getRange().getSheet();
var row=nr.getRange().getRow();
var col=nr.getRange().getColumn();
var rg=sh.getRange(row,col,h+r,w+c);
ss.setNamedRange(name,rg);
break;
}
}
}
Class NamedRange
Class Range
I think one of the approach can be to get that NamedRange and expand it explicitly.
Something like this:
Get that NamedRange using getNamedRanges() :
Set range of that NamedRange including new row using setRange(range)

GAS Class SpreadsheetApp : Append Row and Merge Cell

I wish it was this simple but this doesn't work.
sheet.appendRow(['MERGE', '']).merge();
How can I achieve this?
The method merge() needs to be applied to a range object.
One possibility to do so would be:
function myFunction() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var newRow=sheet.getLastRow()+1;
// the range dimensions should match the value array dimensions
var range=sheet.getRange(newRow,1,1,2);
range.setValues([['MERGE', '']]);
range.merge();
}
References:
getRange()
setValues()
merge()
getLastRow()
Append Row
This is just a simple of example of find a row with todays date and appending it to another sheet. In this example, I also delete the current row in the source sheet this making it more of a move command.
function findingRowsAndAppendingToAnotherSheet() {
var ss=SpreadsheetApp.getActive();
var ssh=ss.getSheetByName('Sheet1');
var dsh=ss.getSheetByName('Sheet2');
var srg=ssh.getRange(2,1,ssh.getLastRow()-1,ssh.getLastColumn());
var vA=srg.getValues();
var cdv=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf();
var d=0;
for(var i=0;i<vA.length;i++) {
var dd=new Date(vA[i][0]);
var ddv=new Date(dd.getFullYear(),dd.getMonth(),dd.getDate()).valueOf();
if(ddv==cdv) {
dsh.appendRow(vA[i]);//Appends row with current date to dsh
ssh.deleteRow(i+2-d++);//Deletes ssh current row. Effectively making this a move instead of a copy
}
}
}
Sheet1 & 2 Before Running
Sheet1 & 2 After Running

Hide/Unhide Columns

I'm looking for help with a macro that will show a set of columns then another that will show that same set of columns that is assigned to two separate buttons.
Currently I have the Hide Bootcamp and Show Bootcamp buttons that will accomplish this task, but I know that columns will need to be added in to this work sheet that will throw off the ranges in the future (<> Column I OR Columns BJ:CO). Does anybody have a solution that will keep the range integrity as columns are added/removed to the worksheet?
Worksheet Link
https://docs.google.com/spreadsheets/d/17H9QMJ7Lmznon8G0dO4MGkidE0b6ymrvgt7fTE9gQBw/edit?usp=sharing
Current Code Below
Hide Columns
function HideBootcamp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('I:I').activate();
spreadsheet.getActiveSheet().hideColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
spreadsheet.getRange('BJ:CO').activate();
spreadsheet.getActiveSheet().hideColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
spreadsheet.getRange('A2').activate();
};
Show Columns
function ShowBootcamp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('I:I').activate();
spreadsheet.getActiveSheet().showColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
spreadsheet.getRange('BJ:CO').activate();
spreadsheet.getActiveSheet().showColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
spreadsheet.getRange('A2').activate();
};
Hide Column By Name
If you answer yes to both of my questions in the comment above then this will hide your column for you. All you have to do is give the function the name of your column in the top row and of course you will have to change the sheet name probably.
function hideColumnByName(name) {
var name=name||'HDR10';//debug
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Master');
var rg=sh.getRange(1,1,1,sh.getLastColumn())
var hA=rg.getValues()[0];
sh.hideColumns(hA.indexOf(name)+1);
}
It is missing activate range.
function hideColumnByName(name) {
var name=name||'HDR10';//debug
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Master');
var rg=sh.getRange(1,1,1,sh.getLastColumn()).activate();
var hA=rg.getValues()[0];
sh.hideColumns(hA.indexOf(name)+1);
}