Finding cell with same value as function from access log - javascript

I've got this piece of code that is supposed to take the function getUserEmail() (That works perfectly) then go through every row of my Access log, and if it finds the same email take the column to the left of it, where the name is. Atm it only returns my else value, which is, if it can't find a user to the email, it just returns the email.
function getUserName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Access List");
var lastRow = sheet.getLastRow();
var userEmail = getUserEmail();
var userName = "";
for (var row = 1; row <= lastRow; row++){
var EmailRange = sheet.getRange(row, 4).getValue();
if(userEmail == EmailRange) {
var userName =+ sheet.getRange(row, 3).getValue();
return userName;
}
else if (userName == ""){
return userEmail;
}
}
}

So if anyone stumbles upon this I got it working by tweaking my first code
function getUserName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Access List");
var lastRow = sheet.getLastRow();
var userEmail = getUserEmail();
//Logger.log(userEmail);
//Logger.log(lastRow);
for (var i = 1; i <= lastRow; i++) {
//Logger.log(sheet.getRange(i,4).getValue());
if(userEmail == sheet.getRange(i,4).getValue()) {
return sheet.getRange(i, 3).getValue();
}
else if(i == lastRow) {
return userEmail;
}
}
}
Works like a charm

Related

Faster way to collect data from JSON than looping in spreadsheet

I am learning Javascript and this is my first time working with Google Sheets Apps Script. What I am doing is taking a large JSON file and importing it into my sheet. After that I am populating a few hundred properties based on the key:value found in the JSON.
This is how it kinda works right now:
Go to first column and first row of my sheet.
Get the name (property name).
Search the JSON for the key and then grab the value.
Update a neighbor cell with the value found in the JSON.
Right now it all works the only issue is it seems to be pretty slow. It takes about .5-1 second per lookup and when I have 200+ properties it just seems slow. This might just be a limitation or it might be my logic.
My sheet can be found here: https://docs.google.com/spreadsheets/d/1tt3eh1RjL_CbUIaPzj10DbocgyDC0iNRIba2B4YTGgg/edit#gid=0
My function that does everything:
function parse() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(2,1);
var range1 = sheet.getRange("A2");
var cell = range.getCell(1, 1);
var event_line = cell.getValue();
var tmp = event_line.split(". ");
var col_number = tmp[0];
var event_name = tmp[1];
event_json = get_json_from_cell(col_number);
const obj = JSON.parse(event_json);
var traits = obj.context.traits;
var properties = obj.properties;
//Get the range for the section where properties are
var traits_range = sheet.getRange("contextTraits");
var allprop = sheet.getRange("testAll");
var alllen = allprop.getNumRows();
var length = traits_range.getNumRows();
for (var i = 1; i < length; i++) {
var cell = traits_range.getCell(i, 1);
var req = traits_range.getCell(i, 4).getValue();
var trait = cell.getValue();
var result = traits[trait];
var result_cell = traits_range.getCell(i, 3);
if (result == undefined) {
if (req == "y"){
result = "MISSING REQ";
result_cell.setBackground("red");
} else {
result = "MISSING";
result_cell.setBackground("green");
}
} else {
result_cell.setBackground("blue");
}
result_cell.setValue(result);
Logger.log(result);
}
for (var i = 1; i < alllen; i++) {
var cell = allprop.getCell(i,1);
var req = allprop.getCell(i, 4).getValue();
var prop = cell.getValue();
var result = properties[prop];
var result_cell = allprop.getCell(i, 3);
if (result == undefined) {
if (req == "y"){
result = "MISSING REQ";
result_cell.setBackground("red");
} else {
result = "MISSING";
result_cell.setBackground("green");
}
} else {
result_cell.setBackground("blue");
}
result_cell.setValue(result);
}
Logger.log(result);
}

How can I have a cell that when edited publishes it's edit date next to it? In Google Sheets using a Google Apps Script

This is what I've got:
function onEdit(e) {
var range = e.range;
var val = range.getValue();
var row = range.getRow();
var col = range.getColumn();
var shift = 1;
var ss = SpreadsheetApp.getActiveSheet().getRange(row, (col+shift));
if (col == 3){
ss.setValue(new Date()).setNumberFormat("MM/dd/yyy hh:mm:ss");
}
if (val == ""){
ss.setValue("").setNumberFormat("MM/dd/yyy hh:mm:ss");
}
}
This is what is happening:
However, when I edit a cell in barcode, it doesn't update the timestamp...
function onEdit(e) {
//e.source.toast("Entry");
//console.log(JSON.stringify(e));
const sh=e.range.getSheet();
if(sh.getName()=="Sheet1" && e.range.columnStart==3 && e.value) {
e.range.offset(0,1).setValue(Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy HH:mm:ss"));
}
}
I presume you wouldn't want to this for all sheets but I could be wrong.

Update orders status Google Spreadsheet by checking the status in other Sheet using Google Apps Scripts

I'm working to get the status for orders to be updated in the master sheet. I have disconnected orders with status in another sheet named "Decommission sheet". While I update the master I need to check the status for these orders by applying the below logic:
If the order is DECOMMISSIONED && not available in decommission sheet
then the order is LIVE.
If the order is available in the decommission sheet then check the
status if it is DECOMMISSIONED then the order is DECOMMISSIONED.
I have done it in a totally different way but it doesn't work with me. Any help would be appreciated.
function pathstatus() {
var MasterSs = SpreadsheetApp.openById('ID');
var MsterSh = MasterSs.getSheetByName('Master Sheet');
var MasterData = MsterSh.getDataRange().getValues();
var DecommisstionSh = MasterSs.getSheetByName('Decommisstion');
var DecommisstionData=DecommisstionSh.getDataRange().getValues();
for(var x=0;x<MasterData.length;x++){
var MasterPathName =MasterData[x][2]
var Masterstatus=MasterData[x][6]
var MasterStage=MasterData[x][7]
if(MasterStage == "DECOMMISSIONED"){
for(var i=0;i<DecommisstionData.length;i++){
var DecommisstionPathName = DecommisstionData[i][2]
var DecommisstionStatus = DecommisstionData[i][7]
var DecommissionedDate = DecommisstionData[i][10]
if(DecommisstionPathName == MasterPathName && DecommisstionStatus == "COMPLETED") {
MasterData[x][6]="DECOMMISSIONED"
MasterData[x][12]=DecommissionedDate
}else {
MasterData[x][6]="LIVE"
}
}
}
}
MsterSh.getRange(2,1,MsterSh.getLastRow(),MsterSh.getLastColumn()).clearContent();
MsterSh.getRange(2,1,MasterData.length,MasterData[0].length).setValues(MasterData)
SpreadsheetApp.flush()
}
In another way
function myFunction() {
var MasterSs = SpreadsheetApp.openById('ID');
var MsterSh = MasterSs.getSheetByName('Master Sheet');
var MasterData = MsterSh.getDataRange().getValues();
var DecommisstionSh = MasterSs.getSheetByName('Decommisstion');
var DecommisstionData=DecommisstionSh.getDataRange().getValues();
MasterData.splice(0,1);
DecommisstionData.splice(0,1);
var Decommisstionpath = [];
var Decommisstionstatus = [];
for(var i=0;i<DecommisstionData.length;i++) {
Decommisstionpath.push(Number(DecommisstionData[i][2]))
Decommisstionstatus.push(DecommisstionData[i][7])
}
var i=0;
for(var x=0;x<MasterData.length && MasterData[x][3] != undefined ;x++) {
var OrderStage = MasterData[x][8]
if(OrderStage=='DECOMMISSIONED') {
var PathName = MasterData[x][2]
var index = Decommisstionpath.indexOf(PathName);
if(index == -1)
{MasterData[x][6]="LIVE" }
else{
MasterData[x][6]="Check"
}
}
}
MsterSh.getRange(2,1,MsterSh.getLastRow(),MsterSh.getLastColumn()).clearContent();
MsterSh.getRange(2,1,MasterData.length,MasterData[0].length).setValues(MasterData)
SpreadsheetApp.flush();
}
Neither function works properly

add dependet values and and return all matched

I would like to add another dependency to look for in arrays. So if a user types the name of a student and the subject list,all the results of the supportlist are showed.
I have setup 3 arrays but i'm not sure how to go further.
gs code
function getStudentInfo(studentName){
//var lookupValue = "support";
var ss = SpreadsheetApp.openByUrl(url);
var ws =ss.getSheetByName("logboek1");
var lc =ss.getLastColumn();
//var data = ws.getRange(1, 1, ws.getLastRow(), 2).getValues()[0];
var data = ws.getRange(1, 1, ws.getLastRow(), lc).getValues();
//var index = data.indexOf(lookupValue) + 1;
var studentNameList = data.map(function(r) { return r[0]; });
var subjectList = data.map(function(r) { return r[1]; });
var supportList = data.map(function(r) { return r[4]; });
//Logger.log(supportList);
var position = studentNameList.indexOf(studentName);
if(position > -1){
return subjectList[position];
js code
}
function getEstimate(){
var zipCode = document.getElementById("zip").value;
if(zipCode){
google.script.run.withSuccessHandler(updateEstimate).getCost(zipCode);
}
}
function updateEstimate(cost){
document.getElementById("est").value =cost;
M.updateTextFields();
}
function getEstimate1(){
var studentName = document.getElementById("zip1").value;
if(studentName){
google.script.run.withSuccessHandler(updatestudentInfo)
.getStudentInfo(studentName);
}
}
function updatestudentInfo(info){
document.getElementById("est1").value =info;
M.updateTextFields();
Wat i would like is to add another dependency. So if a user types the studentName, (if the subjects are available for that student)chooses for the subjectList(that are available for that student) and then gets all of the results of the supportlist that belongs by the student.

javascript google script appendRow fails Service Error

I have a Google Apps Script that has been running for 3 months and starting a few weeks ago I'm getting a "Service Error" on the Appendrow function which I've bolded below. Any ideas on how to fix this?
function updateACOPS2(){
var ss = SpreadsheetApp.openById(".....")
var sheetSubmission = ss.getSheetByName("Sheet8");
var dataSubmission = sheetSubmission.getDataRange().getValues();
var lastColSubmission = sheetSubmission.getLastColumn();
var ssActive = SpreadsheetApp.openById("....")
var sheetActive = ssActive.getSheetByName("AcopsAll");
var sheetMain = ssActive.getSheetByName("Sheet1");
var dataActive = sheetActive.getDataRange().getValues();
var lastrow = sheetActive.getLastRow();
for(var i = 1; i < dataSubmission.length && dataSubmission[i][2] != ""; i++){
var currentIDSubmission = dataSubmission[i][2] + dataSubmission[i][3];
var checkGotMatch = false;
var toCopyRow = sheetSubmission.getRange(i+1,1,1,71);
// copy entire row for new record
Logger.log(currentIDSubmission);
// if there is a matching record flag as matched
for(var j = 1; j<dataActive.length; j++){
var currentIDActive = dataActive[j][2] + dataActive[j][3];
var currentIDSub = dataSubmission[i][2];
if(currentIDSub != '' && currentIDSubmission == currentIDActive){
checkGotMatch = true;
Logger.log(currentIDActive);
break;
}
}
// if it is a new record Append entire row
if(currentIDSub != '' && checkGotMatch == false){
**sheetMain.appendRow(toCopyRow.getValues()[0]);**
}
}
SpreadsheetApp.flush();
ss.toast("ACOPS Active has been updated.", "Complete");
}
In appendRow you need to pass an array so update your appendRow and try
sheetMain.appendRow([toCopyRow.getValues()[0]])

Categories

Resources