why don't match the method signature for SpreadsheetApp.Sheet.getRange - google-apps-script

I want to copy the cell values from b1 to b2 but I'm getting error please help how to fix this
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = ss.getSheetByName("b1");
var source = inputSheet.getRange('B3','D3','F3','H3','J3','L3','N3','P3','R3','T3','V3');
var values =
source.getValues();
var outputSheet = ss.getSheetByName("b2");
outputSheet.getRange(outputSheet.getLastRow()+1,1,values.length,values[0].length).setValues(values);
};
Exception: The parameters (String,String,String,String,String,String,String,String,String,String,String) don't match the method signature for SpreadsheetApp.Sheet.getRange.
recordHistory # RecordPercent.gs:6

You should consider using Sheet.getRangeList()
Sheet.getRange() method has four methods which accept upto four parameters and they are all explained on this page
function getAndSetValueWithRangeList() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('b1');
const osh = ss.getSheetByName('b2');
let rlA = sh.getRangeList(['B3', 'D3', 'F3', 'H3', 'J3', 'L3', 'N3', 'P3', 'R3', 'T3', 'V3']);
rlA.getRanges().forEach((r,i)=>{osh.getRange(r.getA1Notation()).setValue(r.getValue());});
}

Related

Get row values of spreadsheet, only from specified columns using GAS

Try: Get row values of a spreadsheet. from specified columns
function getAllData() {
var url = 'sheetID';
var ss= SpreadsheetApp.openByUrl(url);
var sheet = ss.getSheetByName("Data");
//var range = sheet.getDataRange() //*This gives all data*
I tried by adding following.
var rangeList = sheet.getRangeList(['A1:B', 'E1:F']);
var values = rangeList.getRanges()
Logger.log(values)
return values
}
But log execution says "Info [Range, Range]" .
But I need to display all values of column A, B, E & F in each row.
Please guide me on this.
Just map the one you want:
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2,1,sh.getLastRow() - 1, sh.getLastColumn()).getValues();
const o = vs.map(([a,b,,,e,f]) => [a,b,e,f]);
Logger.log(JSON.stringify(o));
const osh = ss.getSheetByName("Sheet1");
osh.clearContents();
o.unshift(["Product Name","Price1","Price4","UserID"])
osh.getRange(1,1,o.length,o[0].length).setValues(o);
}

App Script: How to set the values of each cell using the .getRangeList?

I am using the .SetValues to attempt to fill every cell I selected through this line var targetSheetRange = targetSheet.getRangeList(arr1);
Unfortunately, when I do it, it always returns me the value of the first cell on all the remaining cells in my Target sheet instead of setting the value of each individual cell from the Source Sheet.
Here's my code:
function filtersCopyData() {
var dTable = SpreadsheetApp.getActiveSpreadsheet();
var sSheetDay = dTable.getSheetByName('Day 1'); // Source Sheet
var sheetRange = sSheetDay.getRangeList(['K3','K4','K5','K6','K7']).getRanges().map(range => range.getValues());
var targetSheet = dTable.getSheetByName('All Filters report'); // Target Sheet
var arr1 = ['B4:C4', 'B6:C6', 'B7:C7', 'B9:C9', 'B10:C10'];
var targetSheetRange = targetSheet.getRangeList(arr1);
targetSheetRange.setValue(sheetRange);
}
K3 value is 9, K4 value is 20, K5 value is 10, K6 value is 10, and K7 value is 10.
targetSheetRange.setValue(sheetRange); When this code is run, all the cells in arr1 return the value of 9, instead of copying the value of each cell from the Source Sheet.
I hope this thing that I'm trying to accomplish does make sense on the code, PS. I'm really a beginner. Thank you everyone!
Description
RangeList is an Array not a Range. I'm suprised your script even ran. You have to use Array.forEach to set the values of the non-contiguous ranges.
You are creating a RangeList and then getting the A1Notation of each range. You can simply define an array of the range A1Notations.
Note, some of the names are different than yours for my test sheet.
Script
function test() {
try {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("Data");
var rangeList = ["A1","A3","A5"];
var values = rangeList.map( range => sheet.getRange(range).getValue() );
rangeList = ["C1:D1","C3:D3","C5:D5"];
rangeList.forEach( (range,index) => sheet.getRange(range).setValues([[values[index],values[index]]]));
}
catch(err) {
console.log(err);
}
}
Reference
https://developers.google.com/apps-script/reference/spreadsheet/sheet#getRangeList(String)
https://developers.google.com/apps-script/reference/spreadsheet/range-list#getRanges()
https://www.w3schools.com/jsref/jsref_map.asp
https://www.w3schools.com/jsref/jsref_foreach.asp
Writing to a rangelist with a rangelist of values
function filtersCopyData() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0'); // Source Sheet
const vs = sh.getRangeList(['K3','K4','K5','K6','K7']).getRanges().map(range => range.getValue());
const tsh = ss.getSheetByName('Sheet1');
const arr1 = ['B4:C4', 'B6:C6', 'B7:C7', 'B9:C9', 'B10:C10'];
const rgl = breakUpRangeList(ss,tsh,tsh.getRangeList(arr1));
const l = rgl.getRanges().length;
rgl.getRanges().forEach((r,i) => {
let a1 = r.getA1Notation();
let idx = i % arr1.length;
r.setValue(vs[idx]);
});
}
function breakUpRangeList(ss=SpreadsheetApp.getActive(),sh=ss.getSheetByName("Sheet0"),rgl) {
let b = [];
rgl.getRanges().forEach(rg => {
rg.getValues().forEach((r,i) => {
let row = rg.getRow() + i;
r.forEach((c, j) => {
let col = rg.getColumn() + j;
b.push(sh.getRange(row, col).getA1Notation())
})
})
})
//Logger.log(JSON.stringify(b));
return sh.getRangeList(b);
}

Google sheets script to export rows to different worksheet

Here is a script I'm using that works, but is trying to import too much data. I apologize for my ignorance when it comes to this.
function copyPaste() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("Print Results");
var rangeSource = source.getDataRange();
var data = rangeSource.getValues();
var lr = rangeSource.getLastRow();
var lc = rangeSource.getLastColumn();
Logger.log(data);
var sss = SpreadsheetApp.openById("XXXXXX");
var target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,lr,lc).setValues(data);
}
What I need this to do is only select A8:M, and only copy over the rows that have a USER ID in column J. The USER ID is always a number, so a simple rule of ">0" should work for that. USER ID is imported from a different tab using a vlookup formula so the cell is not "empty".
Only copy rows where Column J is not null
function copyPaste() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Print Results");
const rg = sh.getDataRange();
const vs = rg.getValues().filter(r => r[9] != '');
Logger.log(vs);
const sss = SpreadsheetApp.openById("XXXXXX");
const target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,vs.length,vs[0].length).setValues(vs);
}
.filter() method

Trying to getlastrow

Hello I am trying to have a continual list that copies values from one sheet to another in google sheets. My problem is that it is overwriting on the second sheet. I can't figure out how to find the last row before it inserts values. Here is my code.
function SubmitData() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Validation')
var drng = sht.getDataRange();
var rng = sht.getRange(6,2, drng.getLastRow(),drng.getLastColumn());
var rngA = rng.getValues();//Array of input values
var rngB = [];//Array where values that past the condition will go
var b = 0;//Output iterator
for(var i = 0; i < rngA.length; i++)
{
rngB[b]=[];//Initial new array
rngB[b].push(rngA[i][0],rngA[i][2]);
b++;
}
var shtout = s.getSheetByName('Track Data');
var outrng = shtout.getRange(2,1, rngB.length,2);//Make the output range the same size as the output array
outrng.setValues(rngB);
I don't see any condition in your code, you're just remapping
function SubmitData() {
const s = SpreadsheetApp.getActive();
const sht = s.getSheetByName('Validation');
const shtout = s.getSheetByName('Track Data');
const rng = sht.getRange(6, 2, sht.getLastRow() - 5, sht.getLastColumn() - 1);
const vs = rng.getValues();
let vO = vs.map(r => [r[0], r[2]]);
shtout.getRange(shtout.getLastRow() + 1, 1, vO.length, vO[0].length).setValues(vO);
}
Here is a modified script, easier to read. If you have the "sheet" in a variable you can call the getLastRow() method. Then plus 1.
You also can push an array inside an array.
Be aware that bad written functions can plus the lastrow. The "" will be see ad NOT empty. For instance:
//BAD:
=IF(A10 = "","","This is not empty")
//GOOD:
=IF(A10 = "",,"This is not empty")
The script:
function SubmitData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const inputSheet = ss.getSheetByName('Validation')
const inputValues = inputSheet.getRange(6,2, inputSheet.getLastRow(),inputSheet.getLastColumn()).getValues()
const output = [];
inputValues.forEach(row => {
output.push([row[0].row[2]])
})
const outputSheet = ss.getSheetByName('Track Data');
outputSheet.getRange(outputSheet.getLastRow() + 1,1,output.length,output[0].length).setValues(output)
}

How to overwrite cell function with Paste value only using Google Script

I have a database that makes live calculations, but once the new data added to the database has been processed that day, I effectively want to Copy + Paste value only on these cells so that the results of the formulas are fixed and can no longer change.
The current script I am using is:
function myFunction() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
var r = ss.getRange(1,2,ss.getLastRow());
var vals = r.getValues();
for(var i =0;i<vals.length;i++){
ss.getRange(i+1,2).setValue(ss.getRange(i+1,2).getValue());
}
}
However, in column B beneath the data set, all the cells start with IF(B2092="","",etc). On cells that are empty, I still require the formula to remain in place. I only want to script to be applied to cells that have data within.
Can anyone help with this? Thanks
Try this:
function myFunction() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getValues(); // get values
var formulas = rg.getFormulas(); // get formulas
// merge values & formulas
var data = formulas.map((row,row_index) =>
row.map((cel, cel_index) =>
{ return (cel) ? cel : vals[row_index][cel_index] }));
rg.setFormulas(data); // output
}
Corrected version:
function copy_values_and_formulas() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getValues(); // get values
var formulas = rg.getFormulas(); // get formulas
// merge values & formulas
var data = vals.map((row, row_index) =>
row.map((cel, cel_index) =>
{ return (cel == 0) ? formulas[row_index][cel_index] : cel }));
rg.setFormulas(data); // output
}
function myFunction() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getDisplayValues();
for(var i=0;i<vals.length;i++){
if(vals[i][1] && vals[i][1].length!=0) {
ss.getRange(i+1,2).setValue(ss.getRange(i+1,2).getValue());
}
}
}