Users input text in cell A1 of sheet1. I want to find an exact match in column G of sheet2, and get the row of that match. I'm new to GAPS and wrote a script to find matches all on one sheet. How do I modify it to look at sheet2?
function rowOfMatch(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var inputName = sheet.getRange("A1").getValue();
for(var i = 0; i<data.length;i++){
if(data[i][6] == inputName){ //[6] to search column G
Logger.log((i+1))
return i+1;
}
}
}
function runOne(){
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Sheet1');
var sh2=ss.getSheetByName('Sheet2');
var rg1=sh1.getRange(1,1,sh1.getLastRow(),1);
var rg2=sh2.getRange(1,7,sh2.getLastRow(),1);
var v1=rg1.getValues();
var v2=rg2.getValues().map(function(r){return r[0];});
for(var i=0;i<v1.length;i++) {
var row=v2.indexOf(v1[i][0])+1;
if(row>0) {
sh1.getRange(i+1,2).setValue(row);
}else{
sh1.getRange(i+1,2).setValue('Not Found');
}
}
}
Animation:
Related
Hello so I am trying to get values from the 'book' sheet then copy it to 'Completed' sheet. It takes the values form book stores it in the data array and copy them to the 'Completed' sheet. Its working fine but very slow which jeopardizes my work in time-wise. How can I make this run faster ?
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders'); // clear the destination sheet first
clear_sheet.getRange('A2:X').clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange('Q3:Q'); //range to check
var Datavalue = (Datarange.getValues());
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (i=0; i<Datavalue.length;i++) {
if ( Datavalue[i] == "Completed") {
data.push.apply(data,sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
var column = dest.getRange('A3:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct][0] != "" ) { // to find the last row correctly, getLastRow is not working perfectly
ct++;
}
var endRow = ct+2;
}
Try this:
Don't use this syntax var values=sheet.getRange('A3:A').getValues() because it get's the data all the way down to getMaxRows(). Instead use var values=sheet.getRange(3,1,sheet.getLastRow()-2,1).getValues().
Also Datavalue[i] is a whole row
function myfunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders');
clear_sheet.getRange(2,1,clear_sheet.getLastRow()-1,24).clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange(3,17,sheet.getLastRow()-2,1); //range to check
var Datavalue = Datarange.getValues();
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (var i=0;i<Datavalue.length;i++) {
if (Datavalue[i]["************You need another index here**************"] == "Completed") { //need another index Datavalue is 2d
data.push(sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
}
I think this version will be a lot faster.
function myfunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var ssh=ss.getSheetByName('book');
var dsh=ss.getSheetByName('Completed_Orders');
dsh.getRange(2,1,dsh.getLastRow()-1,24).clear();
var dv=ssh.getRange(3,1,ssh.getLastRow()-2,24).getValues();//this version just gets this data one time so it should a lot faster.
var data=[];
for (var i=0;i<dv.length;i++) {
if (dv[i][16]=="Completed") {
data.push(dv[i]);
}
}
dsh.getRange(2,1,data.length,data[0].length).setValues(data);
}
I have a sheet with 6 columns. Row 1 is titles and each other sheet has the same titles and columns.
I have tried a couple codes like this that i found online with edits but can get it to correctly work for multiple sheets.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Source');
var testrange = sheet.getRange('F:F');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('target');
var data = [];
var j =[];
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Completed') {
data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
Any Help would be greatly appreciated
Deleting Rows as you collect data that meet criteria
function moveRows() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName('Source');
var vA=sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).getValues();
var csh=ss.getSheetByName('target');
var d=0;//deleted row counter
var data=[];
for(var i=0;i<vA.length;i++) {
if (vA[i][5]=='Completed') {
data.push(vA[i]);
sheet.deleteRow(i+1-d++);//increment deleted row counter as you delete rows
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Sheet getLastRow()
Sheet getLastColumn
I am trying to copy data from one sheet, paste it into another and then delete the row that have a "flag" attached to them (Column D). When I execute this code, it just deletes the flag and not the whole row. What is going on here? The functions seem to execute in the wrong order.
Tried using Utilities.sleep(), as well as
separating both into different functions
using a for loop writing out all the columns I want deleted
applying flush to the functions before executing the next
//copies the code
function myFunction() {
var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
var ss = SpreadsheetApp.openByUrl(url)
var sheet = ss.getSheetByName('Bad Emails Input')
var range = sheet.getRange("K:N")
var values = range.getValues()
var target = ss.getSheetByName("worklist")
target.getRange("A:D").setValues(values)
SpreadsheetApp.flush()
Utilities.sleep(5 * 1000)
}
//deletes the rows with flags in them
function myFunction2() {
var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
var ss = SpreadsheetApp.openByUrl(url)
var sheet = ss.getSheetByName('Bad Emails Input')
var range = sheet.getRange("K:N")
var values = range.getValues()
var target = ss.getSheetByName("worklist")
Utilities.sleep(6 * 1000)
var oldnews = target.getRange("D:D")
for (i = oldnews.length; i > 0; i--){
if (oldnews[i] !== undefined) {
target.getRange(i,1)
target.getRange(i,2)
target.getRange(i,3)
target.getRange(i,4)
}
}
SpreadsheetApp.flush()
}
It's not really clear what your asking. So I just guessed at some of this.
function myFunction() {
var ss = SpreadsheetApp.openById("id");
var sheet = ss.getSheetByName('Bad Emails Input')
var range = sheet.getRange(1,11,sheet.getLastRow(),4);
var values = range.getValues()
var target = ss.getSheetByName("worklist")
target.getRange(1,1,values.length,values[0].length).setValues(values);
SpreadsheetApp.flush();
var d=0;
for(var i=0;i<values.length;i++) {
if(values[i][3]) {
target.deleteRow(i+1-d++);
}
}
}
Try the below code:
function myFunction() {
var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
var ss = SpreadsheetApp.openByUrl(url);
var sheet = ss.getSheetByName('Bad Emails Input');
var range = sheet.getRange("K:N");
var values = range.getValues();
var target = ss.getSheetByName("worklist");
target.getRange("A:D").setValues(values);
SpreadsheetApp.flush();
//If you have a header row change the below to A2:D and count to 2
var data=ss.getSheetByName("worklist").getRange("A:D").getValues(),count=1;
for (i in data) {
var rowdata=data[i];
if (!rowdata[0] || rowdata[0]==''){break;}
if (rowdata[3] == 'flag') {//change the flag to however it is in column D
ss.getSheetByName("worklist").deleteRow(count);
count++;
}
}
}
I've been working on this code and doing some research the last couple of weeks and not having much luck. I'm looking to setup a trigger that will scan Col C of dates and if the date has passed move that specific row to another sheet.
Below is where I'm currently at.
Note that I have today's date stored in cell 'O2' of the 'Contract Language' sheet.
I've gotten this code to work with text, but not with dates.
Any advise is greatly appreciated!
function MovetoPastShows2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Confirmed Deals'); //source sheet
var testrange = sheet.getRange('C:C');
var testvalue = testrange.getValues();
var todaysheet = ss.getSheetByName('Contract Language');
var todaycol = todaysheet.getRange('O2');
var today = todaycol.getValues().valueOf();
var csh = ss.getSheetByName('Confirmed (Past)'); //destination sheet
var data = [];
var j =[];
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] = today) {
data.push.apply(data,sheet.getRange(i+1,1,1,187).getValues());
j.push(i);
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
};
Move Past Rows to Destination Sheet
function MovetoPastShows2() {
var ss = SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Confirmed Deals');
var rg=sh.getDataRange();
var vA=rg.getValues();
var today=new Date(ss.getSheetByName('Contract Language').getRange('O2').getValue()).valueOf();
var dsh=ss.getSheetByName('Confirmed (Past)');
var d=0;
for(var i=0;i<vA.length;i++) {
if (new Date(vA[i][2]).valueOf()<=today) {
dsh.appendRow(vA[i])
sh.deleteRow(i+1-d);
d++;
}
}
}
I have unique ID ('ID1') in column A in sheet1 and I need to compare it with column B in sheet2 where are ID ('ID2') to.
So I need to compare those of ID and check if some of them match, if they are I want to change value of cell in the same row as 'ID1' in sheet1 but in another column, let say C.
function IDValidation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName("Sheet2");
var Sheet1range = sheet1.getRange(2,1,sheet1.getLastRow()); //header is row 1
var Sheet2range = sheet2.getRange(2, 2); //header is row 1
var Sheet1cellValue = Sheet1range.getValues();
var Sheet2cellValue = Sheet2range.getValues();//Look here
for(i = 0; i<Sheet1cellValue .length-1; i++){
if(Sheet1cellValue [i][0] == Sheet2cellValue)
{
ss.getSheetByName("Sheet1").getRange(i+2,3).setValue("Close");
}
}
}
Here are some options.
Try this:
function IDValidation() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh1=ss.getSheetByName('Sheet1');
var sh2=ss.getSheetByName("Sheet2");
var sh1rg=sh1.getRange(2,1,sh1.getLastRow()); //header is row 1
var sh2rg=sh2.getRange(2,2);//header is row 1
var sh1cvA=sh1rg.getValues();
var sh2cV=sh2rg.getValue();//look here
for(var i=0;i<sh1cvA.length-1; i++){
if(sh1cvA[i][0]==sh2cV){
sh1.getRange(i+2,3).setValue("Close");
}
}
}
Sheet1
Sheet2
That seems to work.
I don't know, perhaps you're looking for all matches between Sheet1 Column1 and Sheet2 Column2. That's what this function does. I'm using random numbers between 0 and 50 and not unique Ids so there are many matches. I stopped putting close in column3 and started describing where the matches are.
Here's the code:
function IDValidation1() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh1=ss.getSheetByName('Sheet1');
var sh2=ss.getSheetByName("Sheet2");
var sh1rg=sh1.getRange(2,1,sh1.getLastRow(),1); //header is row 1
var sh2rg=sh2.getRange(2,2,sh2.getLastRow(),1);//header is row 1
var sh1cvA=sh1rg.getValues();
var sh2cvA=sh2rg.getValues();
for(var i=0;i<sh1cvA.length-1; i++){
for(var j=0;j<sh2cvA.length;j++) {
if(sh1cvA[i][0]==sh2cvA[j][0]){
sh1.getRange(i+2,3).setValue(Utilities.formatString('Sheet2 Row:%s == Sheet1 Row:%s = %s',j+2,i+2,sh1cvA[i][0]));
}
}
}
}
Here's Sheet1 after running:
And this is Sheet2: (I picked too many rows and couldn't fit them all on my screen)
I think you're understood everything very well because your code is the answer to my problem :) I added only in row '10' -1 with length to not count the last row in sheet2 which is empty. And I will change setValue of course. Thanks fo all.
function IDValidation1() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh1=ss.getSheetByName('Sheet1');
var sh2=ss.getSheetByName("Sheet2");
var sh1rg=sh1.getRange(2,1,sh1.getLastRow(),1); //header is row 1
var sh2rg=sh2.getRange(2,2,sh2.getLastRow(),1);//header is row 1
var sh1cvA=sh1rg.getValues();
var sh2cvA=sh2rg.getValues();
for(var i=0;i<sh1cvA.length-1; i++){
for(var j=0;j<sh2cvA.length-1;j++) {
if(sh1cvA[i][0]==sh2cvA[j][0]){
sh1.getRange(i+2,3).setValue(Utilities.formatString('Sheet2 Row:%s == Sheet1 Row:%s = %s',j+2,i+2,sh1cvA[i][0]));
}
}
}
}