This question already has answers here:
Google Spreadsheet SCRIPT Check if edited cell is in a specific range
(6 answers)
Is there an existing way in google script to check if an A1 notation is in range of a second A1 notation
(2 answers)
Closed 2 years ago.
I have aRange which I do not know what it contains (not size nor values).
Now I want to check if 'B2:B4' is within aRange and get the value(s) of B2:B4 if indeed it is inside aRange.
I am really frustrated to be unable to figure out how to solve this (to me apparently) trivial requirement.
Thank you for any suggestions.
B2:B4 matches exactly given range:
Try range.getA1Notation():
function myFunction(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const range = sh.getRange('B2:B4'); //given range
if (range.getA1Notation()=='B2:B4'){
var vals = range.getValues().flat(); //flat is optional but it returns 1D list
}
Logger.log(vals)
}
B2:B4 is within given range:
As Cooper suggested in his comment, use getRow(), getColumn(), getWidth(), getHeight():
function myFunction(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const range = sh.getRange('B1:B5'); //given range
if(range.getRow()<=2 && range.getColumn()<=2 && range.getWidth()>=1 && range.getHeight()>=3){
var vals = sh.getRange('B2:B4').getValues().flat(); //flat is optional but it returns 1D list
}
Logger.log(vals)
}
References:
getA1Notation()
Class Range
Related
This question already has answers here:
Best Practices for Multiple OnEdit Functions
(1 answer)
Merging or Combining two onEdit trigger functions
(1 answer)
Closed 3 months ago.
I have two scripts that I am trying to run simultaneously that will organize two different columns based on their cell value. Both functions work, I'm just unsure how to combine the two
Could someone please help me?
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (r.columnStart != 9 || r.rowStart == 1 || e.value == src.getName()) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e.value);
src.getRange(r.rowStart,1,1,9).moveTo(dest.getRange(dest.getLastRow()+1,1,1,9));
src.deleteRow(r.rowStart);
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Opened");
var range = sheet.getRange("J2:J100");
function onEdit(e) {
range.sort([{column: 10, ascending: false}]);
}
I have tried combining both formulas however, I am aware that I cannot have two onEdit functions.
I'm learning google apps script and making a sales project. I want to be able to read a certain column from a single cell into infinity (A1:infinity range for example). getRange functions only work if I give them an endpoint, how can I get around that?
function readAllOfTheDataInAColumn() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet1");
const rg = sh.getRange("A1:A" + sh.getLastRow());
const vs = rg.getValues();
}
The Spreadsheet.getRange() and Sheet.getRange() methods work fine with open-ended ranges. You can get the values in column C2:C until the last row in the spreadsheet with something like this:
const ss = SpreadsheetApp.getActive();
const values = ss.getRange('Sheet1!C2:C').getValues();
To remove blank values, use this pattern:
const values = ss.getRange('Sheet1!C2:C').getValues()
.flat()
.filter(value => value !== '');
Note that this gives a 1D array rather than a 2D array.
This question already has answers here:
Running multiple sheets under one google script, syntax error
(2 answers)
TypeError: Cannot find function includes in object
(5 answers)
Closed 6 months ago.
Could you advise please why I get syntax error for => in google script in the below code? If I remove the filter part (values = values.filter(e=>e[0]);) the functions work well. In another file the code with the filter works fine.
function main(){
moveData();
clearRange1();
}
function moveData () {
var ss = SpreadsheetApp.getActive();
var dailySheet = ss.getSheetByName('Վաճառքի գներ');
var appendSheet = SpreadsheetApp.openById('1fpJpb5buD236iO7sHuANdqkPEd2QEQ-QewIIl7us_UQ').getSheetByName('Sheet1');
var values = dailySheet.getRange("Վաճառքի գներ!3:" + dailySheet.getLastRow()).getValues();
values = values.filter(e=>e[0]); //gets rid of blank rows. filters based on the first column (index 0).
appendSheet.getRange(appendSheet.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}
function clearRange1() { //replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.openById('1bdsEQOq7YeVtu0bxH67zrLqlW7uhHBdu0Uwu6ck87UU').getSheetByName('Sheet1')
sheet.getRange('A3:S').clearContent();
var sheet = SpreadsheetApp.openById('1p53h8P_64WC7hG7IwIYxRcb08uGN25oXrH6mtL7u2lQ').getSheetByName('Sheet1')
sheet.getRange('C4:E').clearContent();
}
This question already has answers here:
Best method to extract selected columns from 2d array in apps script
(2 answers)
custom columns selection google script
(1 answer)
Closed last year.
I'm new to google scripts and I'm trying to copy specific range of columns to another sheet.
This is the code
function helpFunction() {
const sss = SpreadsheetApp.getActiveSpreadsheet();
const ts = sss.getSheetByName('this');
const ss = sss.getSheetByName('source');
const rawData = ss.getRange("A:F").getValues(); // HERE - i just need columns B,C,E,F instead of all from a to f
const frawData=rawData.filter(row=>row[0]>0);
ts.getRange(ts.getLastRow()+1, 1, frawData.length, frawData[0].length).setValues(frawData);
}
If there's someone who could help me I will be more than glad.
This question already has answers here:
Add a timestamp to one column when other columns changed
(2 answers)
Google Spreadsheet SCRIPT Check if edited cell is in a specific range
(6 answers)
Closed 5 years ago.
I am trying to use the following generic script, and only apply it to a specific column. How would I specify this to say, column F within the sheet?
function onEdit(e){
// Set a comment on the edited cell to indicate when it was changed.
var range = e.range;
range.setNote('Last modified: ' + new Date());
}
function onEdit(e) {
var range = e.range;
// if you want to test a specific sheet, un-comment the following the following and move the range test inside this if
/* if (range.getSheet().getName() === 'required sheet') {
// do something
}*/
if (range.getColumn() == 6) {// 6 is for col F
range.setNote('Last modified: ' + new Date());
}
}
This should work. It tests the column of the range if it is F (6 in this case) and sets the note.
To build on Karan's great answer. With this you can modify a row and update a column within the modified row.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1')
var row = e.range.getRow();
// 'A' specifies the columns
var range = sheet.getRange('A'+ row)
Logger.log(range)
range.setNote('Last modified: ' + new Date());
}