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.
Related
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:
How to fix 'TypeError: Cannot call method "getRange" of null' in Google Apps Script
(1 answer)
Google Apps Script, copy one spreadsheet to another spreadsheet with formatting
(5 answers)
Closed 1 year ago.
The goal of this function is to copy a new google sheet from the source spreadsheet into a source target spreadsheet.
The funcion insertNewSheet () is a minimal reproducible example. In this example for semplicity it run only if there is no the same sheet in target spreadsheet.
The problem arises when coping the cell values from source sheet to target sheet: in fact I receive always the same error: "TypeError: Cannot read property 'getRange' of null". I checked the range (Logging log) and really the range is not null. So I am struggling with this problem for many hours and I have not the solution.
function insertNewSheet() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = spreadSheet.getSheetByName('Query_Bando');
var nameOfCell= spreadSheet.getSheetByName('Query_Bando').getRange('A2').getValue();
var sourceValues=sourceSheet.getRange("A13").getDataRegion().getValues();
var cellDataRegion=sourceSheet.getRange("A13").getDataRegion();
var lastRowSource= cellDataRegion.getLastRow();
var lastColumnSource= cellDataRegion.getLastColumn();
var tss=SpreadsheetApp.openById("1bh563zHW0WMIcalWP2t3zG1_sK319JuMZwPsWhucKAs");
var ts=tss.getSheetByName(nameOfCell);
//get A1 notation identifying the range
var A1Range = cellDataRegion.getA1Notation();
var SRange = sourceSheet.getRange(A1Range);
tss.insertSheet(nameOfCell);
var SData = SRange.getValues();
//Logger.log(sourceValues);
Logger.log(lastRowSource);
Logger.log(lastColumnSource);
//Logger.log(SRange);
Logger.log(SData);
Logger.log(A1Range);
ts.getRange(A1Range).activate().setValues(SData);
}
This question already has answers here:
Cannot find method getRange(number,number)?
(1 answer)
Values are correct and within range but still sends an email
(1 answer)
getRange(string) works but getRange(number) fails [duplicate]
(2 answers)
Closed 2 years ago.
When I run an ads-script to call the app-script::spreadsheet API, I get the following error:
var sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var rangeValues = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
==> Cannot find method getRange(number,number,number,number)
How can it be? Only a subset of the app-script sheet api is available from ads-script?
In your code, the variable sheet is an object/instance of the spreadsheet class. However, getRange is a method of the sheet object and can't be applied to the sheet variable.
You need to define a sheet object first. This can be done in many ways, one of them is to define it by using the name of the sheet. Here is the spreadsheet object:
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
Then you can define the sheet object:
var sheet = spreadsheet.getSheetByName("Sheet1");
And now you can get the values of a range :
var rangeValues = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
Adjust Sheet1 to the sheet of your choice. Make sure that a sheet with that name exists.
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
This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 4 years ago.
I wanted to execute a function whenever a cell is edited or added in google spreadsheet.
I have the below function, but it is not working. please help. Also I wanted to call the function Start sync after X seconds. How to give a delay in calling the startSync()
function onEdit(e) {
var range = e.range;
range.setNote('Last modified: ' + new Date());
startSync()
}
whenever I edit a cell , a note (comment) is getting added to the cell , but the startSync is not getting called.
function startSync() {
//Get the currently active sheet
var sheet = SpreadsheetApp.getActiveSheet();
//Get the number of rows and columns which contain some content
var [rows, columns] = [sheet.getLastRow(), sheet.getLastColumn()];
//Get the data contained in those rows and columns as a 2 dimensional array
var data = sheet.getRange(1, 1, rows, columns).getValues();
syncMasterSheet(data);
}
Answer:
Installable Triggers helped me to call StartSync()
You can't use a simple onEdittrigger with most .set methods. You need to use an installed onEdit() trigger instead. See this answer for more information on triggers
To delay the start of the function use Utilities.sleep(milliseconds) before the call to the startSync() function. See more info about the Utilities class here