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;
}
Related
I would like to change my code below to autofill the formula across the page - not autofill down
you can see in the 2 images below I can add the formula to N527 but cannot autofill across the page with code - which is what i am looking for.
So images are before and after of what i am looking to accomplish
function LastColumn(){
var spreadsheet = SpreadsheetApp.getActive();
var LastColumn = spreadsheet.getLastColumn();
//spreadsheet.setActiveSheet(spreadsheet.getSheetByName('MASTER'), true);
spreadsheet.getRange('N527').activate();
spreadsheet.getCurrentCell().setFormula('=sum(N497*N498*N499)/100/250');
var destinationRange = spreadsheet.getActiveRange().offset(0, 0, LastColumn -526);
spreadsheet.getRange("N527:BK527").copyTo(destinationRange);
}
I couldnt get the attached code to work
I am trying to Autofill N527 across to AK527 - IF THE CELL ABOVE HAS DATA.
Thanks in Advance
I believe your goal as follows.
You want to put the formula of =sum(N497*N498*N499)/100/250 from the cell "N527" to the column direction.
In this case, when the row 526 of the same column has the value of carton, you want to put the formula.
You want to achieve this using Google Apps Script.
In this case, how about the following sample script?
Sample script:
function LastColumn() {
var sheetName = "Sheet1"; // Please set the sheet name.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var range = sheet.getRange('N526:526');
var ranges = range.getValues()[0].reduce((ar, r, i) => {
if (r.toLowerCase() == "carton") ar.push(range.offset(1, i, 1, 1).getA1Notation());
return ar;
}, []);
sheet.getRangeList(ranges).setFormulaR1C1('=sum(R[-30]C[0]*R[-29]C[0]*R[-28]C[0])/100/250'); // This formula is like `=sum(N497*N498*N499)/100/250`.
}
In this case, at first, the cells "N526:526" are checked whether the cell has the value of carton. And, when the cell has the value, the formula is put to the row 527.
I used RangeList for this.
References:
getRangeList(a1Notations)
setFormulaR1C1(formula) of Class RangeList
You need to copy the formula from cell already containing the formula to the range to which you want to expand it
This means that your destination range should be "O527:BK527".
Sample:
function autoFill(){
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('MASTER');
var firstCell = sheet.getRange('N527');
firstCell.setFormula('=sum(N497*N498*N499)/100/250');
var destinationRange = sheet.getRange("O527:BK527");
firstCell.copyTo(destinationRange);
}
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)
I have a cell (A1) that updates automatically and I would like to use the next cell (B1) to return the max value seen by cell A1. I know nothing about scripts so I google it and found the following script that works but only if A1 doesn't have a formula in it (manually enter a number in the cell but I don't want that). Is there a way to make this work?
function recordMax() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B1");
var values = range.getValues()[0];
range.setValues([[values[0], Math.max(values[0], values[1])]]);
}
Instead of of changing the values of A1:B1 just change the value of B1.
function recordMax() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B1");
var values = range.getValues()[0];
sheet.getRange("B1").setValue(Math.max(values[0], values[1]));
}
I'm trying to build a script that will allow me to copy and value paste a range from tab "robert" to tab "brendan". However, the range in "robert" is dynamic, so sometimes it will need to copy and paste A1:M3 or A1:M20. I'd also like it to append to the the first rows of the destination. So when run, the script looks for values on the source tab (robert), copies them, and pastes their values at the top of the destination (brendan) without overwriting existing data.
So far I've worked with the below script from a different thread:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange('Robert!A1:M100');
source.copyTo(ss.getRange('Brendan!A4'), {contentsOnly: true});
source.clear();
}
The problem here is the script range is static, and it overwrites anything in the destination. I do like the clearing of the source though and the values paste. Any help would be greatly appreciated!
Define the range using variables. Use getLastRow() and getLastColumn() Instead of hard-coding it.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var robWs = ss.getSheetByName("Robert");
var brenWs = ss.getSheetByName("Brendan");
var robLr = robWs.getLastRow()
var robLc = robWs.getLastColumn()
//range starts at row 1 and column 1 (A1), you may need to change accordingly
var vals = robWs.getRange(1,1,lr,lc).getValues;
//paste range also starts at A1. you can find the lr on this sheet and start the range there
brenWs.getRange(1,1,lr,lc).setValues(vals);
function moveValuesOnly() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Robert');
var rg=sh.getActiveRange();//copies active range for source
var vA=rg.getValues();
dsh=ss.getSheetByName('Brendan');
dsr=4;//destination start row
dsc=1;//destination start col
dsa=2;//destination number of rows to append to
drg=dsh.getRange(dsr,dsc,rg.getHeight(),rg.getWidth());
var dvA=drg.getValues();
vA.forEach(function(r,i){
if(i<dsa){
r.forEach(function(c,j){
//only write data in destination for these rows if destination value is null
if(dvA[i][j]='') {
dvA[i][j]=c;
}
});
}else{
dvA[i][j]=c;
}
})
drg.setValues(dvA);
}
I'm trying to put together a script that cuts a range from a google spreadsheet and pastes it to another spreadsheet. The script works fine as long as the source sheet is the first sheet in my document. How can I fix it so that the script grabs the right sheet no matter where it is in my document?
I'm very very new at this and mostly used code I found online. Probably made a very obvious mistake..
function CopyRangeTo_OtherSheet() {
var sss = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var ss = sss.getSheetByName('Sheet1');
var range = ss.getRange(2,4,sss.getLastRow()-1,9);
var data = range.getValues();
var tss = SpreadsheetApp.openById('1WHoETM3QxV166ndnPW4Qv37Y6SBGPVw-CcEQxNpw0Co');
var ts = tss.getSheetByName('Sheet3');
ts.getRange(ts.getLastRow()+1, 1,50,9).setValues(data);
var spread = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var sheet = spread.getSheetByName("Sheet1");
sheet.getRange("D2:H").clearContent();
}
The number of rows will be different in each sheet. .getLastRow() on a spreadsheet class only returns the last row in first sheet. getLastRow of sheet instead
ss.getLastRow()
Your line var ss = sss.getSheetByName('Sheet1'); implies that you take the sheet with the name Sheet1.
This is the sheet you use to obtain your values from range and subsequently copy them.
If you want to copy / cut and paste values from a different sheet, simply paste the correct name of the sheet in var ss = sss.getSheetByName('PASTE HERE THE NAME OF THE SHEET');
Small annotation:
If the range you want to clear is the same from which you copied the
values, you do not need to define it again. You can simply replace the
lines
var spread = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var sheet = spread.getSheetByName("Sheet1");
sheet.getRange("D2:H").clearContent();
through
range.clearContents();
Also note:
setValues() only works if the range into which the values shall be set has the same dimension like the value range, i.e. the original range.
If your original range is 2,4,ss.getLastRow()-1,9, then the number of rows is ss.getLastRow()-1 and the number of columns 9.
Thus, you need to define
ts.getRange(ts.getLastRow()+1, 1,ss.getLastRow()-1,9).setValues(data);
to adapt the row number dynamically.