Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

function editRecord() {

var myGooglSheet= SpreadsheetApp.getActiveSpreadsheet(); //declare a variable and


set with active google sheet
var shUserForm= myGooglSheet.getSheetByName("User Form"); //delcare a variable
and set with the User Form worksheet
var datasheet = myGooglSheet.getSheetByName("Database"); ////delcare a variable
and set with the Database worksheet

//to create the instance of the user-interface environment to use the messagebox
features
var ui = SpreadsheetApp.getUi();

// Display a dialog box with a title, message, and "Yes" and "No" buttons. The
user can also
// close the dialog by clicking the close button in its title bar.
var response = ui.alert("EDITAR", 'EDITAR INFORMACIÓN?',ui.ButtonSet.YES_NO);

// Checking the user response and proceed with clearing the form if user selects
Yes
if (response == ui.Button.NO)
{return;//exit from this function
}

var str = shUserForm.getRange("F7").getValue();


var values = datasheet.getDataRange().getValues(); //getting the entire values
from the used range and assigning it to values variable

var valuesFound=false; //variable to store boolean value to validate whether


values found or not

for (var i = 0; i < values.length; i++)


{
var rowValue = values[i]; //declaraing a variable and storing the value

//checking the first value of the record is equal to search item


if (rowValue[0] == str) {

var iRow = i+1; //identify the row number

datasheet.getRange(iRow,
1).setValue(shUserForm.getRange("F7").getValue()); //Employee ID
datasheet.getRange(iRow,
2).setValue(shUserForm.getRange("F10").getValue()); //Employee Name
datasheet.getRange(iRow,
3).setValue(shUserForm.getRange("F12").getValue()); //Gender
datasheet.getRange(iRow,
4).setValue(shUserForm.getRange("F14").getValue()); // Email ID
datasheet.getRange(iRow,
5).setValue(shUserForm.getRange("K10").getValue()); //Department
datasheet.getRange(iRow,
6).setValue(shUserForm.getRange("K12").getValue());// Address
datasheet.getRange(iRow,
7).setValue(shUserForm.getRange("K14").getValue());// Address
datasheet.getRange(iRow,
8).setValue(shUserForm.getRange("K16").getValue());// Address

ui.alert(' "Data updated for - Emp #' + shUserForm.getRange("").getValue() +'


"');
//Clearnign the data from the Data Entry Form

shUserForm.getRange("F7").clear();
shUserForm.getRange("F10").clear();
shUserForm.getRange("F12").clear();
shUserForm.getRange("F14").clear();
shUserForm.getRange("K10").clear();
shUserForm.getRange("K12").clear();
shUserForm.getRange("K14").clear();
shUserForm.getRange("K16").clear();

valuesFound=true;
return; //come out from the search function
}
}

if(valuesFound==false){
//to create the instance of the user-interface environment to use the messagebox
features
var ui = SpreadsheetApp.getUi();
ui.alert("INFORMACIÓN NO ENTRRADA");
}

You might also like