Formula works on RMA sheet, Cell A2
After Formula on RMA Sheet
I have a Google sheets workbook filled with different sheets (1 main sheet "HelpDesk Tickets").
In the main sheet "HelpDesk Tickets" Column E there is different data based off of ticket categories (8 total) which have sheets named after the categories.
I use a formula which works when ran from the cell A2 in each of the different sheets based off of the ticket categories column "E" from the main sheet (HelpDesk Tickets). I change the sheet name as needed in the formula.
Ex..
From the sheet named "RMA", cell A2, I enter the Formula which copies All rows from the main sheet "HelpDesk Tickets" based off of Column E with "RMA" as a value in any of the cells into the "RMA" sheet. These sheets are used to keep tickets organized for graphs etc. My issue is I cannot get the formula to work when adding it to Apps Script.
Formula (this works)
=query('HelpDesk Tickets'!A:F, "where E = 'RMA' ",0)
I have tried looking at other forums and sites as I feel this should be really simple to add but cannot figure it out for the life of me.
Sites
Google Support 184049281
Stack Overflow 12036726
I took the example from here and tried to change it with what I need and still did not work
↓ Current Script ↓
function sheetRMA(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A2");
cell.setFormula("=query('HelpDesk Tickets'!A:F, "where E = 'RMA' ",0)");
}
↓ Error ↓
Syntax error: SyntaxError: missing ) after argument list line: 7 file: TC_RMA.gs
From ↓ Current Script ↓ of your updated question, unfortunately, I noticed that you don't use my proposed modification. And, I'm worried that my comment might be low readability when the script is put in the comment. So, I would like to add it as an answer. Please modify your script as follows and test it again.
From:
function sheetRMA(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A2"); cell.setFormula("=query('HelpDesk Tickets'!A:F, "where E = 'RMA' ",0)");
}
To:
function sheetRMA() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A2");
cell.setFormula(`=query('HelpDesk Tickets'!A:F, "where E = 'RMA' ",0)`);
}
Reference:
Template literals
Added:
From your following reply,
this is what is needed and expected. Sheet2 searches sheet1 column E for any row with the word "RMA". If it finds the word "RMA" it copies the row to sheet2
In this case, how about the following sample script?
Sample script:
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("Sheet1");
const sheet2 = ss.getSheetByName("Sheet2");
const values = sheet1.getRange(2, 1, sheet1.getLastRow() - 1, sheet1.getLastColumn()).getValues().filter(r => r[4].includes("RMA"));
if (values.length == 0) return;
sheet2.getRange(sheet2.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
Related
I have a Google Sheets database:
https://docs.google.com/spreadsheets/d/1VzHY8fTq8OsXhpHYHESSSPxeVNOnqxpjcsyWJpbuEOs/edit?usp=sharing
'Base Stats' sheet (Sheet 1) is regularly updated with new rows added. Row 1 is a header row. Row 2 onward columns 5-8 have font colours applied. Column 6 has a drop down list. Columns 7, 12 & 13 have formulas. When a new row is inserted, currently it will not copy the formatting and formula detailed above.
I did some searching and I found this possible answer:
function onOpen(e){
var ui = SpreadsheetApp.getUi()
ui.createMenu("Format sheet").addItem("Format", "setFormat").addToUi()
}
function onEdit(e){ //This is activated each time a modification happens in the sheet
var ss = SpreadsheetApp.getActive()
var sheet =ss.getActiveSheet()
if(sheet.getSheetName() == "Base Stats"){
var entryRange = e.range
var range = sheet.getRange(2,entryRange.getColumn(),1,entryRange.getNumColumns()) //This will be you range to get the formatting from row "1" and corresponding column based on the column being edited
Logger.log(entryRange.getA1Notation())
range.copyFormatToRange(sheet, entryRange.getColumn(), entryRange.getNumColumns()+entryRange.getColumn()-1, entryRange.getRow(), entryRange.getNumRows()+entryRange.getRow()-1)
Logger.log(entryRange.getColumn())
if(entryRange.getColumn() == 12){ //This column value will not be allowed modified except for row 1 of that column
if (entryRange.getRow() != 2){ //The columns in row "1" will be allowed to modified
e.range.setValue((e.oldvalue == undefined? "": e.oldvalue))
}
}
}
}
function setFormat(){
var ss = SpreadsheetApp.getActive()
var sheet = ss.getActiveSheet()
var firstRow = sheet.getRange(1, 1, 1, sheet.getLastColumn())
var dataRange = sheet.getDataRange()
firstRow.copyFormatToRange(sheet, 1, dataRange.getNumColumns(), 2, dataRange.getNumRows())
}
However, I'm not sure what I need to edit in the script to apply to my Google Sheets example? Any help greatly appreciated.
You do not need to copy formulas, change for arrayformula, example in M1
={"Score
(Weighted)";arrayformula((H2:H*2)+(E2:E*1.5)+(F2:F+G2:G))}
immediately apply formatting for all columns (and whole columns) and you won't need to copy formatting rules
I'm trying to create a data snapshot on google sheets that will automatically run when a specific cell is edited. How should I do that? I'm not from coding background but so far I've found 2 sources and tried to combine them but when I tested it out, it's giving me error. I want to copy values from A2 to E2 of Snapshot sheet and append row every time B2 is changed. Please advise.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Snapshot");
var source = sheet.getRange("A2:E2");
var values = source.getValues();
if(e.range.getA1Notation() == 'B2'){
sheet.appendRow(values[0]);
}}
The B2 in Snapshot is a formula which displays the value of F1 in Receipt sheet. So, the onEdit is not triggered on change of name.
The name changes in F1
I tried the below code and it is working
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var rsht=ss.getSheetByName("Receipt");
var sheet = ss.getSheetByName("Snapshot");
var source = sheet.getRange("A2:E2");
var values = source.getValues();
if(e.range.getA1Notation() == 'F1'){
sheet.appendRow(values[0]);
}}
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);
}
Here's my problem : i'm trying to copy a value from one cell in "sheet1" to another cell in "sheet2".
But I want to run this script everyday (using trigger) because the value change every day. So the paste has to be in B2 the first day, B3 the second day, etc. I want to be able to follow the value day by day and have an historic.
Here's my current script :
function backup(){
var sourceSheet = "Portfolio" ;
var sourceRange = "N15" ;
var targetSheet = "Suivi perfs journalières" ;
var targetRange = "B2:B999" ;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var values = sheet.getRange(sourceRange).getValues();
ss.getSheetByName(targetSheet).getRange(targetRange).setValues(values);
I have 2 problem :
My script send me this error : Exception: The number of rows of data does not match the number of rows in the range. There are 1 lines of data, but the range has 998. (line 12, "CopyPastePortfolioValue" file)
I don't know how to paste the value in a new cell everyday (paste in the cell under the previous each day)
Thanks for your help!
I believe your goal as follows.
You want to retrieve the value from the cell N15 in the source sheet and append the value to the target sheet.
Modification points:
When setValues(values) is used, the lengths of rows and columns of values are required to match to the range. In your script, values of var values = sheet.getRange(sourceRange).getValues(); is 1 row and 1 column. But the target range is B2:B999. By this, such error occurs. I think that this is the answer for your 1st question.
In order to append the value to the target sheet, in your case, I would like to propose to use appendRow. I think that this is the answer for your 2nd question.
And, when a value from a cell, you can also use getValue instead of getValues.
When above points are reflected to your script, it becomes as follows.
Modified script:
function backup(){
var sourceSheet = "Portfolio";
var sourceRange = "N15";
var targetSheet = "Suivi perfs journalières";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var value = sheet.getRange(sourceRange).getValue(); // Modified
ss.getSheetByName(targetSheet).appendRow([value]); // Modified
}
References:
setValues(values)
appendRow(rowContents)
I have written a script on Google App Script that will, at one point, get a range from one sheet, copy the values on that range and paste them to another sheet.
I have done so by doing this:
var range = sh.getDataRange().offset(0, 0);
var data = range.getValues();
var ts = SpreadsheetApp.openById('SHEETID').getSheetByName('SHEETNAME')
ts.getRange(ts.getLastRow() + 1, 1, data.length, data[0].length).setValues(data)
I would like, however, to specify the exact range to copy and another exact range to paste the data into.
I would like the range to be copied/pasted to be from columns A to Z whilst still keeping the getLastRow() in the script.
How would I do that?
Thanks for your help.
You can use getRange(a1Notation) to fetch a range and its contents. I usually go for A1 notation. Sample:
// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");
// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
There are other variations of getRange you can use. Choose what you're comfortable with.
For copying and pasting altogether, you can use the copyTo(destination)
// The code below copies the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();