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);
}
Related
I'm new to Apps Script and don't know how I can get my selected cells copied into another worksheet.
I want the following but instead of copying A7:A9 into a new worksheet i want the cells/range that are selected at the time to be copied. the selected cells/range are going to be different each time that i want to use the script.
var rangeList = SpreadsheetApp.getActiveRangeList();
function TestMakro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A7:A9').activate();
spreadsheet.insertSheet(1);
spreadsheet.getRange('Tabellenblatt1!A7:A9').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
thanks
So instead of using SpreadsheetApp.getActive() it's better to define separately the spreadsheet and the active range. you can then get A1 annotation of the active range to find the same area in the new sheet (I assumed that is what you are trying to achieve). Key here is that even if you create a new sheet, it doesn't become Active. Hence you still need to find the same range in it.
Here is the code:
function TestMakro() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const activeRange = spreadsheet.getActiveRange();
const newSheet = spreadsheet.insertSheet(1)
activeRange.copyTo(newSheet.getRange(activeRange.getA1Notation()) , SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false)
}
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;
}
I selected manually a range of data and want to press a button to copy those data to after the last row of data in the same sheet using Google Apps Script but not working at all. How to fix it? The codes are:
var listToCopy = SpreadsheetApp.getActiveSheet().getActiveRange();
var height = listToCopy.getHeight();
var destRow = listToCopy.getLastRow()+1;
listToCopy.copyTo(spreadsheet.getRange(destRow, 0, height));
I believe your goal as follows.
You want to copy the selected range to the last row of the same sheet using Google Apps Script.
For this, how about this answer?
Modification points:
In the case of var destRow = listToCopy.getLastRow()+1;, the last row of the range of listToCopy is retrieved.
In your case, the last row of the sheet is required to be retrieved.
The source range can be copied to the destination range as the start range like getRange(destRow, 1).
The 1st number of range is 1.
spreadsheet is not declared.
When above points are reflected to your script, it becomes as follows.
Modified script:
In order to use this script, please select the range and run the function of myFunction. By this, the selected range is copied to the last row of the same sheet.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var listToCopy = sheet.getActiveRange();
var destRow = sheet.getLastRow() + 1;
listToCopy.copyTo(sheet.getRange(destRow, 1));
}
References:
getLastRow() of Class Sheet
copyTo(destination)
Please see example picture below.
I would like to use Google Sheets script editor to add a Filter view on cell B3, that filters on a custom formula =(B4=$B$1).
The plan is to add a onEdit function that checks whether the value in cell B1 has changed, and if so reset the filter view in cell B3.
This is breaking my brain! I can't seem to find a way to auto update/refresh the filter once my value in B1 has changed.
My code currently looks like this, but this doesn't re-add the custom formula. It just keeps showing me all the non-empty rows.
function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheetname = "Lactation";
var sheet = ss.getSheetByName(sheetname);
var erow = e.range.getRow();
//Logger.log("DEBUG: row = "+erow);
var ecolumn = e.range.getColumn();
//Logger.log("DEBUG: column = "+ecolumn);
if (erow == 1 & ecolumn == 2 ){
// there is a match so the cell is B2
//Logger.log("DEBUG: the cell is B2");
updatefilter();
}
else
{
// there is no match so the cell is NOT I3
//Logger.log("DEBUG: the cell is not I3");
}
}
function updatefilter() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B3').activate();
var criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(['', 'No'])
.build();
spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(2, criteria);
};
You want to modify the basic filter of the cells "B3:B" when the cell "B1" on the sheet Lactation is edited.
In this case, you want to use the OnEdit event trigger.
You want to use =(B4=$B$1), which is the custom formula, as the filter condition.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification points:
You can take advantage of the event object as e of onEdit(e).
In order to use the custom function for the basic filter, you can use whenFormulaSatisfied.
Modified script:
In order to run the script, please edit the cell B1 on the sheet Lactation.
function onEdit(e){
var sheetName = "Lactation";
var editCell = "B1";
var range = e.range;
var sheet = range.getSheet();
if (range.getA1Notation() == editCell && sheet.getSheetName() == sheetName) {
updatefilter(sheet);
} else {
// do something
}
}
function updatefilter(sheet) {
sheet.getRange('B3').activate();
var criteria = SpreadsheetApp.newFilterCriteria().whenFormulaSatisfied("=(B4=$B$1)").build();
var filter = sheet.getFilter();
if (!filter) {
filter = sheet.getRange("B3:B").createFilter();
}
filter.setColumnFilterCriteria(2, criteria);
};
In this script, when the cell "B1" on the sheet of Lactation is edited, the function updatefilter is run by the OnEdit event trigger. And when the basic filter is existing, it is updated. When no filter is existing, the filter is set as new filter. The range of the filter is "B3:B". The custom function of =(B4=$B$1) is used as the criteria.
Note:
In above script, the simple trigger can be used. But if you use the methods for requiring to authorize, when an error occurs, please try to use Installable Triggers.
References:
Simple Triggers
Installable Triggers
Event Objects
whenFormulaSatisfied(formula)
If I misunderstood your question and this was not the direction you want, I apologize.
I have this script running nicely on Google Sheets:
function recordMax() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("K16:L16");
var values = range.getValues()[0];
range.setValues([[values[0], Math.max(values[0], values[1])]]);
}
But, I have this effect here http://screencast.com/t/BOXzC0UZxFZ (short 1 min video show you the issue). Summary of the issue is that when the script runs in forces K16 to just show the current value of the formula and strips out the =sum formula I have in K16.
What do you think I need to do so that when the script runs, it will not automatically strip the K16 formula so K16 will continue updating to new values?
:)
How about the following modification?
Modification point :
In order to retrieve formulas in the cells, it uses getFormulas().
Insert the retrieved formulas.
Modified script :
In the most cases, the following modified script can be used. I think that in your case, the following modified script can be used.
function recordMax() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("K16:L16");
var formula = range.getFormulas()[0]; // Added
var values = range.getValues()[0];
range.setValues([[formula[0], Math.max(values[0], values[1])]]); // Modified
}
There are sometimes the case that it requires to insert the formula using setFormula() as follows.
function recordMax() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("K16:L16");
var formula = range.getFormulas()[0]; // Added
var values = range.getValues()[0];
sheet.getRange("K16").setFormula(formula[0]); // Added
sheet.getRange("L16").setValue(Math.max(values[0], values[1])); // Added
}
Reference :
getFormula()
getFormulas()
setFormula(formula)
setFormulas()
If I misunderstand your question, please tell me. I would like to modify.