I want to identify equal rows in two different sheets on same spreadsheet. I tried below code, it doesn't work.
function getMyEqualRows()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];
var sheetOneValues = sheet1.getRange('A1:G20').getValues();
var sheetTwoValues = sheet2.getRange('A1:G20').getValues();
var equalRows = [];
for (var i = 0; i <= sheetOneValues.length; i++) {
for(var j = 0; j <= sheetTwoValues.length; j++){
if(sheetOneValues[i] == sheetTwoValues[j]) {
equalRows.push(sheetOneValues[i]);
}
}
return equalRows;
}
}
You can't compare two arrays for equality with ==. Instead you can use .join() method. It concatenates all elements in these arrays to a string. This check string arrays for equality. I'm not sure if this is the best way.
function getMyEqualRows()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];
var sheetOneValues = sheet1.getRange('A1:G20').getValues();
var sheetTwoValues = sheet2.getRange('A1:G20').getValues();
var equalRows = [];
for(var i in sheetOneValues)
{
var anEqualRow = false;
for(var j in sheetTwoValues)
{
if(sheetOneValues[i].join() == sheetTwoValues[j].join()){
anEqualRow = true;
}
}
if(anEqualRow){
equalRows.push(sheetOneValues[i]);
}
}
return equalRows;
}
Hope this would work!
Related
everyone!
I'm trying to write a script where the cell G3 on the sheet 'ref view' will change to 'Yes' if the cell D9 on 'view' contains 'BP' when I run it. The problem is that the execution is completed without errors but when I try to test it nothing happens.
This is the code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetInfo = ss.getSheetByName('ref view')
var data = sheet.getDataRange().getValues();
var rangeView = ss.getRange('View!D9');
var cell = ss.getRange('ref view!G3');
var range = rangeView;
for(var i=0; i<data.length; i++) {
if(rangeView == 'BP') {
sheet.getRange(cell).clear().setValue('Yes');
}
}
} ```
Try it this way:
function one() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getActiveSheet();
var vs1 = sh1.getDataRange().getValues();
var sh2 = ss.getSheetByName('ref view')
var sh3 = ss.getSheetByName('view');
var sh3D9v = sh3.getRange('D9').getValue();//value
var sh2G3r = sh2.getRange('G3');//Range
for(var i=0; i<vs1.length; i++) {
if(sh3D9v == 'BP') {
sh1.getRange(sh2G3r).setValue('Yes');
}
}
}
Be aware that a range does not return a value without the use of a getValue() or a getValues();
I need help with finding a number from a specific row in column A and write next to its blank cells in two different columns of the same row. e.g Find a number in COLUMN A2, Write to cells in COLUMN B2 and C2.
I have currently used filters and it hasn't helped.
function filterRange(filteredRange) {
return filteredRange == [2.0];
}
function findCellByValue() {
var ss = getSpreadSheet();
var sheet = ss.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
var range = values[i];
var filtered = range.filter(filterRange);
var newValue = filtered.concat(["Tee", "uhuuu"]);
console.log(newValue);
var ssheet = sheet.getRange("B2:C2");
console.log(ssheet.setValues([newValue]));
}
}
Anyways I have managed to find a workaround it though it is not the best possible solution but for this, it works.
function findCellByValue(cellValue) {
console.log(cellValue);
var ss = getSpreadSheet().getActiveSheet();
var range = ss.getRange("A2:A").getValues();
for(var i = 0; i < range.length; i ++)
{
if(range[i] == cellValue)
{
var Arrayvalues = range[i].concat(["test1","test2"]);
ss.getRange(i+2,1,1,3).setValues([Arrayvalues]);
i = range.length;
}
}
Very new to this, I am trying to Find and Replace on row1 only
I want to find aa and replace it with ZZZ
I am trying two different ways but neither work
Thanks
first way:
function replace3() {
var sheet = SpreadsheetApp.getActiveSheet();
var lc = sheet.getMaxColumns();
var range = sheet.getRange("a1:z1");
var values = range.getValues();
for (var col in values[1]) {
if (values[0][col] ='aa') {
values[0][col] = 'ZZZ';
}
}
range.setValues(values);
}
Second Way:
function replace2() {
var sheet = SpreadsheetApp.getActiveSheet();
var lc = sheet.getMaxColumns();
var range = sheetname.getRange(1, 1, 1, lc);
var values = range.getValues();
for (var i = 0; i <= lc; i++) {
if (values[0][i] ='aa') {
values[0][i] = 'ZZZ';
}
}
range.setValues(values);
}
How about the following modofications?
For replace3()
The index of array starts 0.
When values are compared at if, please use ==.
Modified script :
function replace3() {
var sheet = SpreadsheetApp.getActiveSheet();
var lc = sheet.getMaxColumns();
var range = sheet.getRange("a1:z1");
var values = range.getValues();
for (var col in values[0]) { // Modified
if (values[0][col] =='aa') { // Modified
values[0][col] = 'ZZZ';
}
}
range.setValues(values);
}
For replace2()
sheetname is not declared.
When values are compared at if, please use ==.
Modified script :
function replace2() {
var sheet = SpreadsheetApp.getActiveSheet();
var lc = sheet.getMaxColumns();
var range = sheet.getRange(1, 1, 1, lc); // Modified
var values = range.getValues();
for (var i = 0; i <= lc; i++) {
if (values[0][i] == 'aa') { // Modified
values[0][i] = 'ZZZ';
}
}
range.setValues(values);
}
Other method :
Also you can achieve it using map().
Sample script :
function sample() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("a1:z1");
var values = range.getValues();
var data = values[0].map(function(e){return e == 'aa' ? 'ZZZ' : e});
range.setValues([data]);
}
If I misunderstand your question, please tell me. I would like to modify.
I am working on merging multiple tabs on a google sheet and creating one master sheet.
function combinesheets()
{
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheets = doc.getSheets();
var outSheet = doc.getSheetByName("combinesheets");
for (i in sheets){
if (sheets[i].getSheetName() == "sheet1") or (sheets[i].getSheetName() == "sheet2")
{
var data = getRowsData(sheets[i]);
insertData(outSheet, data);
}
}
}
Using the above script I am getting ReferenceError: getRowsData is not defined.
A line is in the wrong place there Cooper, it won't work.
Try this:
function combinesheets()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allsheets = ss.getSheets();
var combineSht = ss.getSheetByName("combinesheets");
for (var i=0; i<allsheets.length; i++)
{
var shtName = allsheets[i].getName();
if (shtName != 'combinesheets')
{
var sht = ss.getSheetByName(shtName);
var shtrng = sht.getDataRange();
var shtrngA = shtrng.getValues();
for(var j=0;j<shtrngA.length;j++)
{
combineSht.appendRow(shtrngA[j]);
}
}
}
SpreadsheetApp.flush();
}
I rewrote the function for you and it works on my spreadsheet. It's a little slow but it gets the job done.
function combinesheets()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allsheets = ss.getSheets();
var combineSht = ss.getSheetByName("combinesheets");
for (var i=0; i<allsheets.length; i++)
{
if (shtName != 'combinesheets')
{
var shtName = allsheets[i].getName();
var sht = ss.getSheetByName(shtName);
var shtrng = sht.getDataRange();
var shtrngA = shtrng.getValues();
for(var j=0;j<shtrngA.length;j++)
{
combineSht.appendRow(shtrngA[j]);
}
}
}
SpreadsheetApp.flush();
}
I have a function that returned a list of unique names in a spreadsheet column. I was looking for a way that I could make the check not case sensitive without forcing everything lowercase. Here is my original code:
function testGetUniqueClassNames(){
var sheet = getRosterSheet();
var dataRange = sheet.getDataRange();
var indices = returnIndices();
dataRange = dataRange.getValues();
var classNames = getUniqueClassNames(dataRange, indices.clsNameIndex, indices.crfIdIndex);
Logger.log(classNames);
}
function getUniqueClassNames(dataRange, clsNameIndex, crfIdIndex) {
var classNames = [];
for (var i=2; i<dataRange.length; i++) {
var thisClassName = dataRange[i][clsNameIndex];
var thisClassRoot = dataRange[i][crfIdIndex];
if ((classNames.indexOf(thisClassName)==-1)&&(thisClassName!='')&&(thisClassRoot!='')) {
classNames.push(thisClassName);
}
}
classNames.sort();
return classNames;
}
To solve this I created a second lowercase array to check against, but used the original one in the return.
function testGetUniqueClassNames(){
var sheet = getRosterSheet();
var dataRange = sheet.getDataRange();
var indices = returnIndices();
dataRange = dataRange.getValues();
var classNames = getUniqueClassNames(dataRange, indices.clsNameIndex, indices.crfIdIndex);
Logger.log(classNames);
}
function getUniqueClassNames(dataRange, clsNameIndex, crfIdIndex) {
var classNames = [];
var classNamesLC = [];
for (var i=2; i<dataRange.length; i++) {
var thisClassName = dataRange[i][clsNameIndex];
var thisClassNameLC = thisClassName.toLowerCase();
var thisClassRoot = dataRange[i][crfIdIndex];
if ((classNamesLC.indexOf(thisClassNameLC)==-1)&&(thisClassName!='')&&(thisClassRoot!='')) {
classNames.push(thisClassName);
classNamesLC.push(thisClassNameLC);
}
}
classNames.sort();
return classNames;
}