if range in rangelist - google-apps-script

Im trying to find if a range is in a rangeList and I saw somewhere something like the statement:
if ( myrange in myrangelist ) ...
I have tried to get this to work but cannot.
Does someone know if possible and if yes then how?

Perhaps this will help:
function isContainedInActiveRangeList(rgA1) {
var rgA1=rgA1||'B2';
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var list=sh.getActiveRangeList();
var rgA=list.getRanges();
var fulllist=[];
for(var i=0;i<rgA.length;i++) {
var rvA=rgA[i].getValues();
var row=rgA[i].getRow();
var col=rgA[i].getColumn();
for(var j=0;j<rvA.length;j++) {
for(var k=0;k<rvA[j].length;k++) {
fulllist.push(sh.getRange(row+j,col+k).getA1Notation());
}
}
}
var trg=sh.getRange(rgA1);
var tvA=trg.getValues();
var trow=trg.getRow();
var tcol=trg.getColumn();
var cObj={ins:[],outs:[],fully:true,partially:false};
for(var i=0;i<tvA.length;i++) {
for(var j=0;j<tvA[i].length;j++) {
var a1=sh.getRange(trow+i,tcol+j).getA1Notation();
if(fulllist.indexOf(a1)>=0) {
cObj.ins.push(a1);
}else{
cObj.outs.push(a1);
}
}
}
if(cObj.ins.length>0) {
cObj.partially=true;
}
if(cObj.outs.length>0) {
cObj.fully=false;
}
Logger.log(cObj);
Logger.log(fulllist);
return (cObj.fully)?true:false;
}

Related

Apps Script: Move duplicates to another sheet instead of deleting them

I found this amazing masterpiece of #Cooper to search for duplicates based on values in 1 column. Now instead of removing these duplicates as the code does, I want to move them to another sheet. Any idea how this can be done?
Thanks :).
Here is the code:
function removeDuplicates() {
var sh=SpreadsheetApp.getActiveSheet();
var dt=sh.getDataRange().getValues();
var uA=[];
var d=0;
for(var i=0;i<dt.length;i++) {
if(uA.indexOf(dt[i][0])==-1) {
uA.push(dt[i][0]);
}else{
sh.deleteRow(i+1-d++);
}
}
}
Move row
function removeDuplicates() {
const ss = SpreadsheetApp.getActive();
const dsh = ss.getSheetByName('Destination');
var sh=SpreadsheetApp.getActiveSheet();
var dt=sh.getDataRange().getValues();
var uA=[];
let d = 0;
for(var i=0;i<dt.length;i++) {
if(uA.indexOf(dt[i][0])==-1) {
uA.push(dt[i][0]);
}else{
dsh.appendRow(dt[i]);//append
sh.deleteRow(i+1-d++);//delete
}
}
}

How to get the cell position of a button or rather how to get a variable for the execution of a button?

With the function I woud like to do the following:
Flow
1.) Get the current User ID
2.) Finding the user's lines where the states are on the alert icon
3.) Change the date in column K to the current date using the format "dd.MM.yyyy"
4.) Generate a time stamp in column L using the format "HH:mm:ss"
5.) Generate a time stamp in the column P using the format "dd.MM.yyyy' 'HH:mm"
Therefore I used the code below.
Unfortunately the code doesn't work. I did not got any error.
What am I doing wrong?
function CheckAll(){
var userID=getUserId();
if(userID) {
var timezone = "GMT+2";
var TimestampFormat1 = "dd.MM.yyyy' 'HH:mm";
var TimestampFormat2 = "HH:mm:ss";
var ss=SpreadsheetApp.getActiveSpreadsheet();
var tickerCheck=ss.getSheetByName('WATCHLIST-Pflege');
var State=tickerCheck.getRange("O1:O2").getCell(2,1).getValues();
var startRow=16;
var lastRow=tickerCheck.getLastRow()-startRow+1;
var rngStateCheck=tickerCheck.getRange(startRow,15,lastRow,1);
var StateCheck=rngStateCheck.getValues();
var userIds=tickerCheck.getRange(startRow,1,lastRow,1).getValues();
var rngLastCheck=tickerCheck.getRange(startRow,11,lastRow,1);
var rngTimeStamp1=tickerCheck.getRange(startRow,12,lastRow,1);
var rngTimeStamp2=tickerCheck.getRange(startRow,16,lastRow,1);
var date=new Date();
for(var i=0;i<StateCheck.length;i++){
if(StateCheck[i][14]==State && userIds[i][0]==userID){
rngLastCheck.getCell(i+1, 1).setValue(date);
rngTimeStamp1.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat2));
rngTimeStamp2.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat1));
}
}
}else{
SpreadsheetApp.getUi().alert('Falsche User ID');
}
}
function getUserId() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Startseite');
var rg=sh.getRange(35,3,sh.getLastRow()-3,3);
var vA=rg.getValues();
var userEmail=Session.getEffectiveUser().getEmail();
for(var i=0;i<vA.length;i++) {
if(vA[i][0]==userEmail) {
return vA[i][1];
break;
}
}
return null;
}
I solved the problem by adding the variable var StateToCheck=StateCheck[i][0] to the for loop.
Now the code looks like this:
function CheckAll(){
var userID=getUserId();
if(userID) {
var timezone = "GMT+2";
var TimestampFormat1 = "dd.MM.yyyy' 'HH:mm";
var TimestampFormat2 = "HH:mm:ss";
var ss=SpreadsheetApp.getActiveSpreadsheet();
var tickerCheck=ss.getSheetByName('WATCHLIST-Pflege');
var State=tickerCheck.getRange("O1:O2").getCell(2,1).getValues();
var startRow=16;
var lastRow=tickerCheck.getLastRow()-startRow+1;
var rngStateCheck=tickerCheck.getRange(startRow,15,lastRow,1);
var StateCheck=rngStateCheck.getValues();
var userIds=tickerCheck.getRange(startRow,1,lastRow,1).getValues();
var rngLastCheck=tickerCheck.getRange(startRow,11,lastRow,1);
var rngTimeStamp1=tickerCheck.getRange(startRow,12,lastRow,1);
var rngTimeStamp2=tickerCheck.getRange(startRow,16,lastRow,1);
var date=new Date();
for(var i=0;i<StateCheck.length;i++){
var StateToCheck=StateCheck[i][0]
if((StateToCheck==State)&&(userIds[i][0]==userID)){
rngLastCheck.getCell(i+1, 1).setValue(date);
rngTimeStamp1.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat2));
rngTimeStamp2.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat1));
}
}
}else{
SpreadsheetApp.getUi().alert('Falsche User ID');
}
}
function getUserId() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Startseite');
var rg=sh.getRange(35,3,sh.getLastRow()-3,3);
var vA=rg.getValues();
var userEmail=Session.getEffectiveUser().getEmail();
for(var i=0;i<vA.length;i++) {
if(vA[i][0]==userEmail) {
return vA[i][1];
break;
}
}
return null;
}
You could use this approach:
function CheckAll1(){
var userID=getUserId();
if(userID) {
var ss=SpreadsheetApp.getActive();
var tsh=ss.getSheetByName('Tickerprüfung');
var startRow=12;
var lastRow=tsh.getLastRow()-startRow+1;
var range=tsh.getRange(startRow,12,tsh.getLastRow()-startRow+1,1);
var tvA=range.getValues();
var userIds=tsh.getRange(startRow,1,tsh.getLastRow()-startRow+1,1).getValues();
var lcrg=tsh.getRange(startRow,9,tsh.getLastRow()-startRow+1,1);
var tsrg=tsh.getRange(startRow,10,tsh.getLastRow()-startRow+1,1);
var today=new Date();
var dateInMs=new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
for(var i=0;i<tvA.length;i++){
var nextDate=new Date(tvA[i][0]);
if(Object.prototype.toString.call(nextDate) === '[object Date]' && userIds[i][0]==userID) {
if(dateInMs>=nextDate.getTime()){
lcrg.getCell(i+startRow, 1).setValue(today);
tsrg.getCell(i+startRow, 1).setValue(new Date().toLocaleTimeString());
}
}
}
}else{
SpreadsheetApp.getUi().alert('Invalid UserId');
}
}
function getUserId() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Startseite');
var rg=sh.getRange(20,2,sh.getLastRow()-19,2);
var vA=rg.getValues();
var userEmail=Session.getEffectiveUser().getEmail();
for(var i=0;i<vA.length;i++) {
if(vA[i][0]==userEmail) {
return vA[i][1];
break;
}
}
return null;
}
And then in the Startseite sheet you could associate user emails with UserId's in this way.
Look closely at the code as I have corrected some errors pertaining to the calculation of rows that did not account for startRow=12 also third parameter in a range is the number of rows not the last row.

Date compare If statement not working, see what is wrong

I want to compare two dates and see what is similar but the if doesn't
work. I want to create a comparison with a cell value and a column of values.
function jobLogSchedule () {
var ss = SpreadsheetApp.getActiveSpreadsheet();/// Activate Sheet Application
var formSS = ss.getSheetByName("Form"); //Form Sheet
var dataBase = ss.getSheetByName("Data"); //Data Sheet
var viewSchedule = formSS.getRange("H2").getValue();
var firstDate = new Date(viewSchedule);
formSS.getRange("H2").setValue(firstDate);
formSS.getRange("H4").setValue(firstDate);
var lastRow = dataBase.getLastRow();
for(i=1; i<lastRow; i++){
var workingCell = dataBase.getRange(i,6,lastRow);
var dateDataBase = workingCell.getValue();
if (dateDataBase == viewSchedule) {
}
else {
}
}
}
try this:
function jobLogSchedule () {
var ss=SpreadsheetApp.getActive();
var fsh=ss.getSheetByName("Form");
var dsh=ss.getSheetByName("Data");
var lastRow=dsh.getLastRow();
var drg=dsh.getRange(1,6,lastRow,1);
var vA=drg.getValues();
var firstDate = new Date(fsh.getRange("H2").getValue());
fsh.getRange("H2").setValue(firstDate);
fsh.getRange("H4").setValue(firstDate);
for(i=0;i<vA.length;i++){
if (new Date(vA[i][0]).valueOf()==firstDate.valueOf()) {
//true
}
else {
//false
}
}
}

How to delete specific cells with app script?

My intention is to be able to delete for example cell F1 without deleting cell F2.
var sheet = ss.getSheetByName("Questionário DQSA");
var range = sheet.getRange("B1:U1");
range.clearContent();
Here I clear the content of these cells but I would like to know how to delete them.
You can use Range.deleteCells(Dimension):
var sheet = ss.getSheetByName("Questionário DQSA");
var range = sheet.getRange("F1");
range.deleteCells(SpreadsheetApp.Dimension.COLUMNS);//F2 becomes the new F1
If you want to roll your own:
function deleteCell(shift) {
var shift=shift || "left"; //left or up
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var cell=ss.getActiveCell();
if(shift=="left") {
var rg=sh.getRange(cell.getRow(),1,1,sh.getLastColumn());
var vA=rg.getValues();
var row=vA[0];
row.splice(cell.getColumn()-1,1);
rg.clearContent();
sh.getRange(cell.getRow(),1,1,row.length).setValues([row]);
}
if(shift=="up") {
var rg=sh.getRange(1,cell.getColumn(),sh.getLastRow(),1);
var vA=rg.getValues();
var row=vA.map(function(r){return r[0]});
row.splice(cell.getRow()-1,1);
var col=[];
row.forEach(function(e){
col.push([e]);
});
rg.clearContent();
sh.getRange(1,cell.getColumn(),col.length,1).setValues(col);
}
}
Of course now that I know about that new method in range it's a lot easier to write. Thanks to #TheMaster
function deleteCell(shift) {
var shift=shift || "left";
var ss=SpreadsheetApp.getActive();
var cell=ss.getActiveCell();
if(shift=="left") {
cell.deleteCells(SpreadsheetApp.Dimension.COLUMNS);
}
if(shift=="up") {
cell.deleteCells(SpreadsheetApp.Dimension.ROWS);
}
}

Change moverows script that checks blanks i a row to specific cells left blank in a row

Got help with my script from Cooper yet i need some modification, the script basically moverows with a custom dialog if a cell in the row is left blank.
What i like to accomplish is that only the specific cells in a row will be checked.
e.g. row 1 and 4 are mandatory and row 2 and 3 are not.
This is the code what I've got so far.
function Moverows57654() {
var ss=SpreadsheetApp.getActive();
var sourceSheet=ss.getSheetByName("sheet1");
var targetSheet=ss.getSheetByName("sheet2");
var sourceRange=sourceSheet.getRange("A2:T2");
var values=sourceRange.getValues();
var range0=sourceSheet.getRange("A2:B2");
var range1=sourceSheet.getRange("D2:E2");
var firstFreeRow=goToFirstRowAfterLastRowWithData(targetSheet, "A:AD");
var noBlanks=true;
var bA=[];
var ro=sourceRange.getRow();
var co=sourceRange.getColumn();
for(var ri=0;ri<values.length;ri++) {
for(var ci=0;ci<values[ri].length;ci++) {
if(!values[ri][ci]) {
noBlanks=false;
bA.push(sourceSheet.getRange(ri+ro,ci+co).getA1Notation());
}
}
}
if(noBlanks) {
targetSheet.getRange(firstFreeRow,1,values.length,values[0].length)
.setValues(values); targetSheet.getRange(firstFreeRow, 1, values.length, values[0].length)
.setValues(values);
range0.clearContent();
range1.clearContent();
//createPdf()//function gets started
}else{
SpreadsheetApp.getUi().alert('Sorry there are blanks in the following cells: ' + bA.join(', '));
return;
}
Try this:
function Moverows57654() {
var ss=SpreadsheetApp.getActive();
var sourceSheet=ss.getSheetByName("sheet1");
var targetSheet=ss.getSheetByName("sheet2");
var sourceRange=sourceSheet.getRange("A2:T2");
var values=sourceRange.getValues();
var range0=sourceSheet.getRange("A2:B2");
var range1=sourceSheet.getRange("D2:E2");
//var firstFreeRow=targetSheet.getLastRow() + 1;
var firstFreeRow=goToFirstRowAfterLastRowWithData(targetSheet, "A:AD");
var noBlanks=true;
var bA=[];
var skA=[1,2];//column -1
var ro=sourceRange.getRow();
var co=sourceRange.getColumn();
for(var ri=0;ri<values.length;ri++) {
for(var ci=0;ci<values[ri].length;ci++) {
if(!values[ri][ci] && skA.indexOf(ci)==-1) {
noBlanks=false;
bA.push(sourceSheet.getRange(ri+ro,ci+co).getA1Notation());
}
}
}
if(noBlanks) {
targetSheet.getRange(firstFreeRow,1,values.length,values[0].length)
.setValues(values); targetSheet.getRange(firstFreeRow, 1, values.length, values[0].length)
.setValues(values);
range0.clearContent();
range1.clearContent();
//createPdf()//function gets started
}else{
SpreadsheetApp.getUi().alert('Sorry there are blanks in the following cells: ' + bA.join(', '));
return;
}
}