Set value first column in row if id match in array - javascript

I'm trying to set checkboxes in a range. The firebase_id array must match column B in the range. If matching? Set row to TRUE. But i'm get some randomly checked checboxxes..
What am I doing wrong..?
function setCheckboxIfValueKinguinIdExist() {
const AS = SpreadsheetApp.getActiveSpreadsheet();
const SHEET_TESTING = AS.getSheetByName("Testing");
let get_firebase_items = getSelectedIdsFromFirebase();
let firebase_ids = get_firebase_items.map(function(item){return item.kinguinId}); // [ '17','2962','9798']
let last_row = SHEET_TESTING.getLastRow();
let values = SHEET_TESTING.getRange("A2:B"+last_row).getValues();
let row = 1;
for(let a in values) {
let item = values[a][1];
if(firebase_ids.includes(item) === true){
SHEET_TESTING.getRange(row,1).setValue("TRUE");
}
row++;
}
}

Try this:
function setCheckboxIfValueKinguinIdExist() {
const AS = SpreadsheetApp.getActiveSpreadsheet();
const SHEET_TESTING = AS.getSheetByName("Testing");
let get_firebase_items = getSelectedIdsFromFirebase();
let firebase_ids = get_firebase_items.map(function(item){return item.kinguinId}); // [ '17','2962','9798']
var last_row = SHEET_TESTING.getLastRow();
var values = SHEET_TESTING.getRange("A2:B"+last_row).getValues();
var row = 1;
values.forEach((r,i)=>{
if(firebase_ids.includes(r[1])) {
SHEET_TESTING.getRange(i+2,1).setValue("TRUE");
}
});
}

Related

Set values from different sheets to another sheet

In summary, I'm trying to simplify this function that load values from two different sheets to another sheet.
All the values are stored in rows in two sheets (DBClienti and DataBkp), all these rows have a reference cell with a unique ID. I select an ID from DBClienti and the function find the relative row number, corresponding to the data to load in the last sheet (Quota).
I'm setting this data using all those vars, but of course there is a better (and right) way that I don't know.
function loadDataBkp() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheetQuota = ss.getSheetByName('Quota');
const sheetDBClienti = ss.getSheetByName("DBClienti");
const sheetDataBkp = ss.getSheetByName("DataBkp");
//Reset Inputs
resetQuota();
//Select the ID DOC
var selectedIDDoc = sheetDBClienti.getActiveCell();
var selectedIDDocVal = selectedIDDoc.getValue();
//Find row of ID DOC in DBClienti
var rowDBClienti;
const dataDBClienti = sheetDBClienti.getDataRange().getValues();
for(var i = 0; i<dataDBClienti.length;i++){
if(dataDBClienti[i][9] == selectedIDDocVal){
rowDBClienti = i+1;
}
}
//Set values in Quota - list
var valI4 = sheetDBClienti.getRange(rowDBClienti,1).getValue();
var valI5 = sheetDBClienti.getRange(rowDBClienti,2).getValue();
var valI6 = sheetDBClienti.getRange(rowDBClienti,3).getValue();
var valI7 = sheetDBClienti.getRange(rowDBClienti,4).getValue();
var valI8 = sheetDBClienti.getRange(rowDBClienti,5).getValue();
var valI9 = sheetDBClienti.getRange(rowDBClienti,6).getValue();
var valI10 = sheetDBClienti.getRange(rowDBClienti,7).getValue();
var valI11 = sheetDBClienti.getRange(rowDBClienti,8).getValue();
sheetQuota.getRange('I4').setValue(valI4);
sheetQuota.getRange('I5').setValue(valI5);
sheetQuota.getRange('I6').setValue(valI6);
sheetQuota.getRange('I7').setValue(valI7);
sheetQuota.getRange('I8').setValue(valI8);
sheetQuota.getRange('I9').setValue(valI9);
sheetQuota.getRange('I10').setValue(valI10);
sheetQuota.getRange('I11').setValue(valI11);
//Find row of ID DOC in DataBkp
var rowDataBkp;
const dataDataBkp = sheetDataBkp.getDataRange().getValues();
for(var i = 0; i<dataDataBkp.length;i++){
if(dataDataBkp[i][0] == selectedIDDocVal){
rowDataBkp = i+1;
}
}
//Set values in Quota - sections
var valC2 = sheetDataBkp.getRange(rowDataBkp,2).getValue();
var valC4 = sheetDataBkp.getRange(rowDataBkp,3).getValue();
var valC5 = sheetDataBkp.getRange(rowDataBkp,4).getValue();
var valC6 = sheetDataBkp.getRange(rowDataBkp,5).getValue();
var valC7 = sheetDataBkp.getRange(rowDataBkp,6).getValue();
var valC8 = sheetDataBkp.getRange(rowDataBkp,7).getValue();
var valC9 = sheetDataBkp.getRange(rowDataBkp,8).getValue();
var valC10 = sheetDataBkp.getRange(rowDataBkp,9).getValue();
var valC11 = sheetDataBkp.getRange(rowDataBkp,10).getValue();
var valC12 = sheetDataBkp.getRange(rowDataBkp,11).getValue();
var valF4 = sheetDataBkp.getRange(rowDataBkp,12).getValue();
var valF5 = sheetDataBkp.getRange(rowDataBkp,13).getValue();
var valF8 = sheetDataBkp.getRange(rowDataBkp,14).getValue();
var valF9 = sheetDataBkp.getRange(rowDataBkp,15).getValue();
var valF12 = sheetDataBkp.getRange(rowDataBkp,16).getValue();
var valF13 = sheetDataBkp.getRange(rowDataBkp,17).getValue();
var valF25 = sheetDataBkp.getRange(rowDataBkp,18).getValue();
var valF26 = sheetDataBkp.getRange(rowDataBkp,19).getValue();
var valF27 = sheetDataBkp.getRange(rowDataBkp,20).getValue();
var valI14 = sheetDataBkp.getRange(rowDataBkp,21).getValue();
sheetQuota.getRange('C2').setValue(valC2);
sheetQuota.getRange('C4').setValue(valC4);
sheetQuota.getRange('C5').setValue(valC5);
sheetQuota.getRange('C6').setValue(valC6);
sheetQuota.getRange('C7').setValue(valC7);
sheetQuota.getRange('C8').setValue(valC8);
sheetQuota.getRange('C9').setValue(valC9);
sheetQuota.getRange('C10').setValue(valC10);
sheetQuota.getRange('C11').setValue(valC11);
sheetQuota.getRange('C12').setValue(valC12);
sheetQuota.getRange('F4').setValue(valF4);
sheetQuota.getRange('F5').setValue(valF5);
sheetQuota.getRange('F8').setValue(valF8);
sheetQuota.getRange('F9').setValue(valF9);
sheetQuota.getRange('F12').setValue(valF12);
sheetQuota.getRange('F13').setValue(valF13);
sheetQuota.getRange('F25').setValue(valF25);
sheetQuota.getRange('F26').setValue(valF26);
sheetQuota.getRange('F27').setValue(valF27);
sheetQuota.getRange('I14').setValue(valI14);
}
Try it this way:
function loadDataBkp() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh1 = ss.getSheetByName('Quota');
const sh2 = ss.getSheetByName("DBClienti");
const sh3 = ss.getSheetByName("DataBkp");
resetQuota();
var v2 = sh2.getActiveCell().getValue();
var row1;
sh2.getDataRange().getValues().forEach((r,i) => { if (r[9] == v2) { row1 = i + 1; } })
sh1.getRange(4, 9, 8).setValues(sh2.getRange(row1, 1, 1, 8).getValues().flat().map(v => [v]))
var row2;
const vs3 = sh3.getDataRange().getValues().forEach((r, i) => { if (r[0] == v2) { row2 = i + 1 } })
let xvs = sh3.getRange(row2, 2, 1, 20).getValues().flat();
['C2', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'F4', 'F5', 'F8', 'F9', 'F12', 'F13', 'F25', 'F26', 'F27', 'I14'].forEach((s, i) => { sh1.getRange(s).setValue(xvs[i]) });
}

Variable value not being saved in function

I want my filters variable to update, my guess is it's re-initializing as the set value every time the function is called, whenever i try to declare it outside of the function I get a lexical error, how can I make sure it keeps the value assigned to it after a button has clicked
export function categoryRender(){
let filter = 'RICK'
console.log(filter)
const all = document.getElementById('all');
all.onclick = function(){
filter = 'ALL'
render(filter);
}
categories = categories.sort();
const filterContainer = document.getElementById("filter-container");
filterContainer.innerHTML = "";
const allFilterImg = document.getElementById('all-image');
if (filter === 'ALL'){
allFilterImg.setAttribute('src', './images/checked.jpeg')
}else{
allFilterImg.setAttribute('src', './images/unchecked.png')
console.log('unchecked all firing')
}
for (let i = 0; i < categories.length; i++){
const line = document.createElement("span");
const filterButton = document.createElement("img");
const filterLabel = document.createElement("h2");
filterContainer.appendChild(line);
line.appendChild(filterButton);
line.appendChild(filterLabel);
line.setAttribute('id', categories[i]);
line.classList.add('filter-line');
filterLabel.innerHTML = categories[i];
if (filter === categories[i]){
filterButton.setAttribute('src', './images/checked.jpeg')
}else{
filterButton.setAttribute('src', './images/unchecked.png')
}
line.onclick = function(){
filter = categories[i];
render(filter)
}
}
}

How to use unmanaged drag on ag-grid?

Is there any examples that show how to make a single column grid sortable and draggable? (unmanaged dragging)
The onRowDragMove event doesn't fire properly.
Each row is an object with only two key-value pairs: name and path. This is what I've tried
function onRowDrag(e) {
console.log('row drag()')
let movingNode = e.node;
let overNode = e.overNode;
let rowStore = e.api.getRenderedNodes();
console.dir(rowStore);
let rowNeedsToMove = movingNode !== overNode;
if (rowNeedsToMove) {
// the list of rows we have is data, not row nodes, so extract the data
let movingData = movingNode.data;
let overData = overNode.data;
let fromIndex = rowStore.indexOf(movingData);
let toIndex = rowStore.indexOf(overData);
let newStore = rowStore.slice();
moveInArray(newStore, fromIndex, toIndex);
rowStore = newStore;
e.api.setRowData(newStore);
e.api.clearFocusedCell();
}
function moveInArray(arr, fromIndex, toIndex) {
var element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
}
}

Why does my loop doesn't work on GoogleSheets?

So, I've been trying to do this: https://wafflebytes.blogspot.com/2017/06/google-script-create-calendar-events.html
I've pasted this code, and it worked quite nicely (the only downside is that the eventID always took the last column, instead of the one I choose).
Then I wrote this code:
function createCalendar() {
var sheet = SpreadsheetApp.getActiveSheet();
var calendar = CalendarApp.getCalendarById('YES MY ID IS HERE BUT I REMOVED TO POST');
var startRow = 2;
var numRows = sheet.getLastRow();
var numColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows-1, numColumns);
var data = dataRange.getValues();
var complete = "on";
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var aplicador = row[2];
var data = new Date(row[9]);
var datafim = new Date(row[11]);
var eventID = row[22];
if (eventID != complete) {
var currentCell = sheet.getRange(startRow + i, numColumns);
calendar.createEvent(aplicador, data, datafim);
currentCell.setValue(complete);
}
}
console.log(data.length)
}
which is exactly the same code, except I changed some variables. And it doesn't work.
It does well on the first line, but the loop doesn't happen.
Why is that?
You declared data twice once inside the loop and once for all of the data on the spreadsheet.
Try it this way:
function createCalendar() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const calendar=CalendarApp.getCalendarById('YES MY ID IS HERE BUT I REMOVED TO POST');
const shsr=2;
const rg=sh.getRange(shsr,1,sh.getLastRow()-shsr+1,sh.getLastColumn());
var data=rg.getValues();
const complete="on";
const lc=sh.getLastColumn();
data.forEach(function(r,i){
let aplicador=r[2]
let dts=new Date(r[9])
let dte=new Date(r[11]);
let eventId=r[22];
if(eventId!="on") {
calendar.createEvent(aplicador,dts, dte);
sh.getRange(shsr+i,22).setValue("on")+
}
});
}

How to get sorted unique values from all the values of a column in Google spreadsheet using javascript

I need help to get a list of unique sorted value of a specific column (column B).
This is my try (It doesn't work), can you help me to find the error?
I'm not able to get the correct range in [object] format...
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function UniqueRange() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Prova')
var drng = sht.getDataRange();
var rng = sht.getRange(2,2, drng.getLastRow()-1,1);
var rngA = rng.getValues();
var shtout = s.getSheetByName('Nuovo');
var rngout = shtout.getRange(2,1,rngA.length,1)
var rngB = rngout.setValues(rngA);
var i = 0; i < rngA.length; i++
var rngB = rngB[i];
var unique = rngB.filter( onlyUnique );
}
Solution by Bellian:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function uniquevalues(){
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Prova')
var drng = sht.getDataRange();
var rng = sht.getRange(2,2, drng.getLastRow()-1,1); //range that contain all values
var rngA = rng.getValues();
var unique = [];
for(var i = 0; i < rngA.length; i++) {
var unique = unique.concat(rngA[i+1]);
}
unique = unique.filter(onlyUnique);
Logger.log(unique)
}
function getUniqueSortedArray(sheetname,column,datastartrow)
{
if(sheetname && column && datastartrow)
{
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName(sheetname);
var cntRg=cntSh.getDataRange().sort({column:column,ascending:true});
var vA=cntRg.getValues();
var uiA=[];
for(var i=datastartrow-1;i<vA.length;i++)
{
if(uiA.indexOf(vA[i][column-1])==-1)
{
uiA.push(vA[i][column-1]);
}
}
return uiA;
}
else
{
return 'Invalid Inputs';
}
}
And this is my modification of your code.
function UniqueSortedRange()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName('Prova')
var rg=sh.getDataRange().sort({column:2,ascending:true});
var vA=rg.getValues();
var sh0=ss.getSheetByName('Nuovo');
var luneeko=[];
for(var i=1;i<vA.length;i++)
{
if(luneeko.indexOf(vA[i][1])==-1)
{
luneeko.push(vA[i][1]);
}
}
return luneeko;
}

Categories