I'm having a problem with finding conditional values and replacing them in a Google sheet data array.
I have an array of data in column A.
I want to find the values without the "." is an integer
And add a . and any natural number.
Write new data in column B.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName('Sheet1')
var range = sheet.getRange('a:a')
}
I am new with GAS, and this topic will help me so much,
Thank you for give me a help.
function myFunction() {
const n = 123;
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Sheet1');
const row = sheet.getLastRow();
const range = sheet.getRange(1, 1, row, 1);
const values = range.getValues();
const results = values.map(e => {
if (Number.isInteger(e[0])) {
return [`${e[0]}.${n}`];
}
else {
return [''];
}
});
sheet.getRange(1, 2, row, 1).setValues(results);
}
Related
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);
}
So I understand this is very similar to:
Set Notes from Column A based on contents of Column B
function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var values_range = sheet.getRange("R5:R6");
var notes_range = sheet.getRange("AS5:AS6");
var notes = notes_range.getValues();
values_range.setNotes(notes)
}
However it's returning blank for me. It seems to be because the cells are the results of a SUM.
Is there a way to get the result?
Copy display values in column B as notes in column A
function addNote() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0")
const vs = sh.getRange(2,2,sh.getLastRow() - 1).getDisplayValues();
vs.forEach((r,i)=>{
sh.getRange(i + 2,1).setNote(r[0])
});
}
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);
}
As the title describes, however seems to be running into some type of unknown error. Am I just losing my mind here? Or does this have to do with the getValues being a 2D array.
function AppendCopy() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Calculation');
var ref = ss.getSheetByName("Dashboard").getRange("B2").getValue();
var sh2=ss.getSheetByName(ref);
var A=sh1.getRange('A2:C').getValues();
sh2.appendRow([new Date(),A]);
}
A is a two dimensional array so I'm not sure what you're trying to accomplish. Also when you use ranges like A2:C in script you get nulls between lastrow and maxrows which normally have to be be removed with filter method.
Try is this way:
function AppendCopy() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Calculation');
var ref = ss.getSheetByName("Dashboard").getRange("B2").getValue();
var sh2 = ss.getSheetByName(ref);
var A = sh1.getRange(2,1,sh1.getLastRow()-1,3).getValues().flat().join(', ');
sh2.appendRow([new Date(), A]);
}
I turned A into a string because it's not clear what you are trying to do. appendRow() is supposed to just append one row.
After additional consideration perhaps this is what you were looking to do:
function AppendCopy() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Sheet1');
var ref = ss.getSheetByName("Sheet2").getRange("B2").getValue();
var sh2 = ss.getSheetByName(ref);
var A = sh1.getRange(2, 1, sh1.getLastRow() - 1, 3).getValues().map(r => { r.unshift(new Date()); return r; });
if (sh2.getLastRow() > 1) {
sh2.getRange(sh2.getLastRow() + 1, 1, A.length, A[0].length).setValues(A);
} else {
sh2.getRange(1, 1, A.length, A[0].length).setValues(A);
}
}
This latter solution will append a range of data to the bottom of sh2 with the date inserted into the first column of each row.
Is it possible to copy values after applying a filter?
I want to ignore the hidden values.
I need to filter a sheet with more than 2000 rows and if I use a loop it takes a long time.
Then, I use this:
var filteredRangefec = range.createFilter()
.setColumnFilterCriteria(6,filterCriteria)
.setColumnFilterCriteria(9, filterCriteriafecha)
.getRange();//range.getFilter().remove();
}
But when i use GetValues take all values, filter and not filter
If you want to retrieve the data and manipulate it in Google Apps Script, you could create temporary sheet, copy filtered data to temporary sheet using method:copyTo() with copyPasteType PASTE_NORMAL and use method:getDataRange() & method:getValues() to retrieve the data.
Example Data:
I copied TheMaster answer here and added some features:
function getFilteredValues(){
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = activeSpreadsheet.getSheetByName("Temporary");
//check if existing, delete if yes
if (newSheet != null) {
activeSpreadsheet.deleteSheet(newSheet);
}
//create new sheet with name Temporary
newSheet = activeSpreadsheet.insertSheet();
newSheet.setName("Temporary");
var dataSheet = activeSpreadsheet.getSheetByName("Sheet1");
var toFilter = dataSheet.getDataRange();
var filter = toFilter.createFilter();
//create criteria
var criteria = SpreadsheetApp.newFilterCriteria();
criteria.whenNumberGreaterThan(1200);
//filter first column using the criteria above
filter.setColumnFilterCriteria(1, criteria.build());
//copy filtered data to temporary sheet
var sourceRange = dataSheet.getFilter().getRange();
sourceRange.copyTo(
newSheet.getRange('A1'),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
false);
Logger.log(newSheet.getDataRange().getValues());
activeSpreadsheet.deleteSheet(newSheet);
}
Output:
Note: You can also use this to paste the data to sheet.
This will do it. You just pass the range to the removeFilteredData() function and it will return the filtered array.
/**
* #param {SpreadsheetApp.Spreadsheet.Range} range
* #returns {Array<Array>}
*/
function removeFilteredValues(range) {
const values = range.getValues();
const firstRow = range.getRow();
const sheet = range.getSheet();
const filteredValues = values.filter((row, i) => {
return !(sheet.isRowHiddenByFilter(i + firstRow));
});
return filteredValues;
}
function main() {
const range = SpreadsheetApp
.getActiveSpreadsheet()
.getActiveSheet()
.getDataRange();
const result = removeFilteredValues(range);
}
The following is what I use from #Nikko idea above. You can copy the result to different spreadsheet.
function copyFilteredData(sourceID, sheetName, desID, desSheetName, filterCol, filterValue){
const sourceSS = SpreadsheetApp.openById(sourceID);
const sheet = sourceSS.getSheetByName(sheetName);
// remove filter if any
if (sheet.getFilter()) {
sheet.getFilter().remove();
}
var toFilter = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var filter = toFilter.createFilter();
// create criteria
var criteria = SpreadsheetApp.newFilterCriteria();
criteria.whenTextEqualTo(filterValue);
// filter first column using the criteria above
filter.setColumnFilterCriteria(filterCol, criteria.build());
// copy filtered data
const destinationSS = SpreadsheetApp.openById(desID);
const destSheet = destinationSS.getSheetByName(desSheetName);
var sourceRange = sheet.getFilter().getRange();
sourceRange.copyTo(
destSheet.getRange('A1'),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
false);
// clear our filter before leaving
if (sheet.getFilter()) {
sheet.getFilter().remove();
}
}