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();
}
Related
This question already has an answer here:
Cannot find method getRange(number,number)?
(1 answer)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
To be clear, I looked at other posts in Stackoverflow on this issue before posting the problem. So far nothing has helped so i am reaching out to the community to help. The error is:
Error Exception: The parameters () don't match the method signature
for SpreadsheetApp.Spreadsheet.getRange.
Here is what i have in a function called code.gs
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName("Main");
var configSheet = spreadsheet.getSheetByName("Config");
function createid()
{
var colnum = getColIndexByNameandRow("UID",ss,1)
};
UID is an actual row header in my spreadsheet, and I want to return the column number from the sheet. I developed a function that returns the column Index. The function is listed below
function getColIndexByNameandRow(colName,mysheet,rs)
{
/*
ColName is the name of the column your looking for, string value
mysheet is the recordset for the sheet you are searching for the column
(R)ow (S)tart (rs) is the row position where you want the column search to start
*/
if (rs==undefined) rs=1;
var numColumns = mysheet.getLastColumn();
var row = mysheet.getRange(rs, 1, 1, numColumns).getValues();
for (i in row[0]) {
var name = row[0][i];
if (name == colName)
{
return parseInt(i) + 1;
}
}
return -1;
};
I know my ss active sheet declaration is working because the line before it var numColumns = mysheet.getLastColumn(); works fine and returns the number of rows. The following line chokes every time it runs:
var row = mysheet.getRange(rs, 1, 1, numColumns).getValues();
Whats interesting is I am using the same function with another project, and its flawless n no issues.
I have tried changing .getValues() to .getDisplayValues() with no success.
Anyone got anything else I can try.
Thanks in advance!
I was able to resolve this problem. I renamed the ss SpreadsheetApp to the mainsheet, and it worked. I am not sure why that is, but it doesn't seem to like the use of ss as a SpreadsheetApp. I added my sample test that worked
function test(){
var val = getColIndexByNameandRow("UID",mainsheet,1)
Logger.log(val)
}
This question already has an answer here:
Script to run on specific sheet
(1 answer)
Closed 6 months ago.
I'm trying to put together a code and want the code to choose a selected sheet rather than the active sheet - the Sheet that I want the code to run from has been renamed 'Training Record':
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
The rest of the code is as follows and runs as intended without the change that I need:
function createTrainingDocs() {
var tempID ='1dyQIK3OCPWXDLydg-MKRhL43CACtyQQDG3VUR11PlH8'
var folderID = '1UpVdcyuP-c_PZVx8uEBRVEH7m_zBnzvb'
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
//console.log(data)
data.forEach((row,i)=>{
console.log(row[2]+', '+row[1]+' Training Log')
var newDocID = DriveApp.getFileById(tempID).makeCopy(row[2]+', '+row[1]+' Training Log',DriveApp.getFolderById(folderID)).getId()
var body = DocumentApp.openById(newDocID).getBody()
body.replaceText('{{Name}}',row[1]+' '+row[2])
body.replaceText('{{School Name}}',row[0])
SpreadsheetApp.getActiveSheet().getRange(i+1,4).setValue(newDocID)
})
}
Change
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
To
var spread = SpreadsheetApp.getActiveSpreadsheet();
var data = spread.getSheetByName("Training Record").getDataRange().getValues()
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:
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:
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