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]) });
}
Related
Is it possible, depending on the environment of the company, that some features of Google are non-existent? I tried some functions with my personal email and it works; but, in a professional environment it doesn't work.
function getDataForSearch() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("PEP FORMULAR");
return ws.getRange(6, 1, ws.getLastRow()-1, 6).getValues();
}
var data;
function setDataForSearch(){
google.script.run.withSuccessHandler(function(dataReturned){
data = dataReturned.slice();
}).getDataForSearch();
}
function search(){
var searchInput = document.getElementById("searchInput").value.toString().toLowerCase().trim();
var searchWords = searchInput.split(/\s+/);
//var searchColumns = [0,1,2,3,4,5];
var searchColumns = [0];
//and
var resultsArray = searchInput == "" ? [] : data.filter(function(r){
return searchWords.every(function(word){
return searchColumns.some(function(colIndex){
return r[colIndex].toString().toLowerCase().indexOf(word) != -1;
});
});
});
var searchResultsBox = document.getElementById("searchResults");
var templateBox = document.getElementById("rowTemplate");
var template = templateBox.content;
searchResultsBox.innerHTML = "";
resultsArray.forEach(function(r){
var tr = template.cloneNode(true);
var typeInitiativeColumn = tr.querySelector(".type-initiative");
var pepRefColumn = tr.querySelector(".pep-ref");
var projectNameColumn = tr.querySelector(".project-name");
pepRefColumn.textContent = r[0];
typeInitiativeColumn.textContent = r[2];
projectNameColumn.textContent = r[5];
searchResultsBox.appendChild(tr);
});
}
This code works but, when I try to reproduce it in a professional environment, the function setDataForSerach() is not able to get the data from the function getDataForSearch() and make a copy.
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");
}
});
}
I'm new to GAS. I'm trying to accomplish four tasks in a single spreadsheet with three separate sheets to relate to.
I want to copy data from the 'rawData sheet', set it in an array, rearrange the array, and set the values in the 'destination sheet'. And keep it active for new submissions (i.e. getLastRow/appendRow).
In 'destination sheet' I want to color code based on status.
Create a button in 'destination sheet' to mark row(case) complete and move from 'destination' to the 'complete sheet'.
And add onEdit triggers for each.
Your input is greatly appreciated.
Thanks.
function colorCode() {
var range = outerArray;
Logger.log(range);
var row = dBoard.getRange(dBoard.getLastRow(),1,1,dBoard.getLastColumn());
var submitter = range[3];
var status = range[11];
Logger.log(submitter);
Logger.log(status);
//var column = range.getColumn();
//var row = range.getRow();
if (status === "Approved"){
var rowToColor = row.offset(0,0,1,13);
rowToColor.setBackground("#6bb2ff");
}
else if (status === "Requester"){
var rowToColor = row.offset(0,0,1,13);
rowToColor.setBackground("#ffe16b");
}
else if (range === 12 && range > 1 && status === "Denied"){
var rowToColor = row.offset(0,0,1,13);
rowToColor.setBackground("#f9441b");
}
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source1 = ss.getSheetByName("Copy of rawData");
var dBoard = ss.getSheetByName("dBoard2");
//function copyData() {
//var rawD = source1.getRange(1,1,source1.getLastRow(),source1.getLastColumn()).getValues();
//Logger.log(rawD);
function setData() {
var rawD = source1.getRange(source1.getLastRow(),1,1,source1.getLastColumn()).getValues();
//Logger.log(limits);
var columnA = rawD[0][0];
var columnB = rawD[0][18];
var columnC = rawD[0][13];
var columnD = rawD[0][2];
var columnE = rawD[0][3]
var columnF = rawD[0][4];
var columnG = rawD[0][5];
var columnH = rawD[0][6];
var columnI = rawD[0][7];
var columnJ = rawD[0][8];
var columnK = rawD[0][9];
var columnL = rawD[0][11];
var columnM = rawD[0][12];
var outerArray = [columnA,
columnB,
columnC,
columnD,
columnE,
columnF,
columnG,
columnH,
columnI,
columnJ,
columnK,
columnL,
columnM];
Logger.log(columnA);
Logger.log(columnB);
Logger.log(outerArray);
dBoard.getRange(2,1,outerArray.length,outerArray.length);
dBoard.appendRow(outerArray);
}
I have a problem debugging the code. It's working if I define values by myself instead of taking it from properties and spreadsheet. I am new to JavaScript so it can be really basic error.
What I am trying to do with this function:
Taking name of person, date and name of school from the spreadsheet
Getting arrays data from the properties that was saved with other function. The saving line in that function looks like this:
PropertiesService.getScriptProperties().setProperty('Mokyklos', JSON.stringify(Mokyklos));
It's 6 arrays of full names, array of date, array of school names and array of numbers. The numbers array is used to return the answer.
function ApmokMokMokykloje(mokytojas, data, mokykla) {
Utilities.sleep(Math.random() * 1000);
var MokytojaiL = PropertiesService.getScriptProperties().getProperty('MokytojaiL');
var Mokytojai1 = PropertiesService.getScriptProperties().getProperty('Mokytojai1');
var Mokytojai2 = PropertiesService.getScriptProperties().getProperty('Mokytojai2');
Utilities.sleep(Math.random() * 1000);
var Mokytojai3 = PropertiesService.getScriptProperties().getProperty('Mokytojai3');
var Mokytojai4 = PropertiesService.getScriptProperties().getProperty('Mokytojai4');
var Mokytojai5 = PropertiesService.getScriptProperties().getProperty('Mokytojai5');
Utilities.sleep(Math.random() * 1000);
var Datos = PropertiesService.getScriptProperties().getProperty('Datos');
var Mokyklos = PropertiesService.getScriptProperties().getProperty('Mokyklos');
var ApmokMokSkaiciai = PropertiesService.getScriptProperties().getProperty('ApmokMokSkaiciai');
var mokytojaiL = MokytojaiL;
mokytojaiL = JSON.parse(mokytojaiL);
var mokytojai1 = Mokytojai1;
mokytojai1 = JSON.parse(mokytojai1);
var mokytojai2 = Mokytojai2;
mokytojai2 = JSON.parse(mokytojai2);
var mokytojai3 = Mokytojai3;
mokytojai3 = JSON.parse(mokytojai3);
var mokytojai4 = Mokytojai4;
mokytojai4 = JSON.parse(mokytojai4);
var mokytojai5 = Mokytojai5;
mokytojai5 = JSON.parse(mokytojai5);
var datos = Datos;
datos = JSON.parse(datos);
var mokyklos = Mokyklos;
mokyklos = JSON.parse(mokyklos);
var skaicius = ApmokMokSkaiciai;
skaicius = JSON.parse(skaicius);
for(var i = 0; i < mokyklos.length; i++) {
if(data==datos[i] && mokykla==mokyklos[i]) {
if ((mokytojas==mokytojaiL[i]) || (mokytojas==mokytojai1[i]) || (mokytojas==mokytojai2[i]) || (mokytojas==mokytojai3[i]) || (mokytojas==mokytojai4[i]) || (mokytojas==mokytojai5[i])) {
return skaicius[i]; // returns blank
}
}
}
}
The code below works fine.
function testas3(mokytojas, data, mokykla) {
// the same values from spreadsheet
var datapvz="2017-11-04T22:00:00.000Z";
var mokyklapvz="Zalioji mokykla";
var mokytojaspvz="Penivilas Gremlinavičius";
// the same values from properties
var datos = [];
datos[0]="2017-11-08T22:00:00.000Z";
datos[1] = "2017-11-15T22:00:00.000Z";
datos[2] = "2017-11-11T22:00:00.000Z";
datos[3] = "2017-11-03T22:00:00.000Z";
datos[4] = "2017-11-04T22:00:00.000Z";
var mokyklos = [];
mokyklos[0] = "Mokykla nuostabioji";
mokyklos[1] = "Mylimiausioji mokyklele";
mokyklos[2] = "Dar viena mokyykla";
mokyklos[3] = "Raudonoji";
mokyklos[4] = "Zalioji mokykla";
var mokytojaiL = [];
mokytojaiL[0] = "Cristopher Rangel";
mokytojaiL[1] = "Alessandra Knox";
mokytojaiL[2] = "Germtautas Falalavičius";
mokytojaiL[3] = "Lenkgaudė Trikojytė";
mokytojaiL[4] = "Penivilas Gremlinavičius";
var mokytojai1 = [];
mokytojai1[0] = "Mantvydas Špukys";
mokytojai1[1] = "Išeikbaida Visursėkmytė";
mokytojai1[2] = "Svaidgaudė Praperduvienė";
mokytojai1[3] = "Mantvinas Žirgmyla";
mokytojai1[4] = "Mantvinas Žirgmyla";
var mokytojai2 = [];
mokytojai2[0] = "Griovbaida Nepriteklytė";
mokytojai2[1] = "Išeikbaida Visursėkmytė";
mokytojai2[2] = "Griovbaida Nepriteklytė";
mokytojai2[3] = "Arjautauta Fragmentavičiutė";
mokytojai2[4] = "Kastuvaldas Parašiutauskas";
var mokytojai3 = [];
mokytojai3[0] = "Rustautas Celiulionis";
mokytojai3[1] = "Androbauda Parankpapaitė";
mokytojai3[2] = "Arjauvilė Katrakojytė";
mokytojai3[3] = null;
mokytojai3[4] = "Rustautas Celiulionis";
var mokytojai4 = [];
mokytojai4[0] = null;
mokytojai4[1] = null;
mokytojai4[2] = null;
mokytojai4[3] = "Arjauvilė Katrakojytė";
var mokytojai5 = [];
var skaicius = [];
skaicius[0]="99";
skaicius[1]="98";
skaicius[2]="87";
skaicius[3]="89";
skaicius[4]="89";
for(var i = 0; i < mokyklos.length; i++) {
if(datapvz==datos[i] && mokyklapvz==mokyklos[i]) {
if ((mokytojaspvz==mokytojaiL[i]) || (mokytojaspvz==mokytojai1[i]) || (mokytojaspvz==mokytojai2[i]) || (mokytojaspvz==mokytojai3[i]) || (mokytojaspvz==mokytojai4[i]) || (mokytojaspvz==mokytojai5[i]))
{
return skaicius[i]; // returns 89
}
}
}
}
I think the problem is that you're comparing a Date object (from the spreadsheet...e.g., from cell F$1) with a string literal (from your saved JSON object). To make them match, you should call toJSON() on the date object before comparing, like this:
var date = data.toJSON();
for(var i = 0; i < mokyklos.length; i++) {
if(date==datos[i] && mokykla==mokyklos[i]) {
if ((mokytojas==mokytojaiL[i]) || (mokytojas==mokytojai1[i]) || (mokytojas==mokytojai2[i]) || (mokytojas==mokytojai3[i]) || (mokytojas==mokytojai4[i]) || (mokytojas==mokytojai5[i])) {
return skaicius[i];
}
}
}
I have a code sample where I want to read some data from input fields and write that data to a different div.
In first section the code works properly, but in 2nd section the value is not shown. I used the alert function to check that wether value is received or not. The value is received but not shown.
What is wrong with my code? (below)
function preview() {
$(".preview_block").fadeIn(1000);
$("body").css("overflow","hidden");
$(".preview_block").css("overflow","auto");
/*======================Value fetch for personal details=======================*/
var fastname1 = document.forms["myform"]["fastname"].value;
if(fastname1==""){fastname1="---null---"}
var lastname1 = document.forms["myform"]["lastname"].value;
if(lastname1==""){lastname1="---null---"}
var sex1 = document.forms["myform"]["radio"].value;
if(sex1==""){sex1="---null---"}
var clgid1 = document.forms["myform"]["clgid"].value;
if(clgid1==""){clgid1="---null---"}
var dob1 = document.forms["myform"]["dob"].value;
if(dob1==""){dob1="---null---"}
var fname1 = document.forms["myform"]["fname"].value;
if(fname1==""){fname1="---null---"}
var mname1 = document.forms["myform"]["mname"].value;
if(mname1==""){mname1="---null---"}
var gname1 = document.forms["myform"]["gname"].value;
if(gname1==""){gname1="---null---"}
var mobno1 = document.forms["myform"]["mobno"].value;
if(mobno1==""){mobno1="---null---"}
var pmobno1 = document.forms["myform"]["pmobno"].value;
if(pmobno1==""){pmobno1="---null---"}
var mail1 = document.forms["myform"]["mail"].value;
if(mail1==""){mail1="---null---"}
var addr11 = document.forms["myform"]["addr1"].value;
if(addr11==""){addr11="---null---"}
var addr21 = document.forms["myform"]["addr2"].value;
if(addr21==""){addr21="---null---"}
var city1 = document.forms["myform"]["city"].value;
if(city1==""){city1="---null---"}
var state1 = document.forms["myform"]["state"].value;
if(state1==""){state1="---null---"}
var pcode1 = document.forms["myform"]["pcode"].value;
if(pcode1==""){pcode1="---null---"}
var ps1 = document.forms["myform"]["ps"].value;
if(ps1==""){ps1="---null---"}
var po1 = document.forms["myform"]["po"].value;
if(po1==""){po1="---null---"}
var country1 = document.forms["myform"]["country"].value;
if(country1==""){country1="---null---"}
document.getElementById("p1").innerHTML = fastname1;
document.getElementById("p2").innerHTML = lastname1;
document.getElementById("p3").innerHTML = sex1;
document.getElementById("p4").innerHTML = clgid1;
document.getElementById("p5").innerHTML = dob1;
document.getElementById("p6").innerHTML = fname1;
document.getElementById("p7").innerHTML = mname1;
document.getElementById("p8").innerHTML = gname1;
document.getElementById("p9").innerHTML = mobno1;
document.getElementById("p10").innerHTML = pmobno1;
document.getElementById("p11").innerHTML = mail1;
document.getElementById("p12").innerHTML = addr11;
document.getElementById("p13").innerHTML = addr21;
document.getElementById("p14").innerHTML = city1;
document.getElementById("p15").innerHTML = state1;
document.getElementById("p16").innerHTML = pcode1;
document.getElementById("p17").innerHTML = ps1;
document.getElementById("p18").innerHTML = po1;
document.getElementById("p19").innerHTML = country1;
/*======================Value fetch for XII standered details=======================*/
var qualification1 = $('#qualification option:selected').html();
if(qualification1==""){qualification1="---null---"}
var q1_board1 = $('#q1_board option:selected').html();
if(q1_board1==""){q1_board1="---null---"}
var clg_nm1 = document.forms["myform"]["clg_nm"].value;
if(session1==""){session1="---null---"}
var regno1 = document.forms["myform"]["regno"].value;
if(regno1==""){regno1="---null---"}
var yr_reg1 = document.forms["myform"]["yr_reg"].value;
if(yr_reg1==""){yr_reg1="---null---"}
var rollno1 = document.forms["myform"]["rollno"].value;
if(rollno1==""){rollno1="---null---"}
document.getElementById("q1").innerHTML = qualification1;
document.getElementById("q2").innerHTML = q1_board1;
document.getElementById("q3").innerHTML = clg_nm1;
document.getElementById("q4").innerHTML = regno1;
document.getElementById("q5").innerHTML = yr_reg1;
document.getElementById("q6").innerHTML = rollno1;
}