Agan2 pecinta programming, gw butuh pencerahan nih... gw lagi coba2 lagi coding setelah 15 tahun berhenti....
Gw lagi coba bikin simple web via Google Sheets -> Script editor
Sederhana sih :
1. User input authorization code / ID di Search Box
2. Kalau diklik search, sistem nyari di google sheetsnya terus nampilin profile data pribadi berdasarkan ID tersebut.
Nah gw udah coba develop ini 2 minggu... satu per satu tantangan gw berhasil lewati kecuali satu ini... pas diklik tombol search, halaman html jadi putih tapi di-debug gak ada error... gw coba masukin windows.alert(parameter) di function terakhir sebelum tampil di web itu keluar
Spoiler for "Code.gs":
Code:
/**
* Special function that handles HTTP GET requests to the published web app.
* @return {HtmlOutput} The HTML page to be served.
*/
function doGet() {
return HtmlService.createTemplateFromFile('Index').evaluate()
.setTitle('Hello World')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
/**
* Returns information about the personal data based on a particular user id.
* @param {String} userId The ID of the user.
* @return {Array.<Object>} The personal data information.
*/
function getPersonalData(userId)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var masterlistSheet = ss.getSheetByName("Masterlist");
var masterlistData = masterlistSheet.getRange(1, 1, masterlistSheet.getLastRow(), masterlistSheet.getLastColumn()).getValues();
var personalData = [];
var rowCounter = 1;
var columnCounter = 1;
for (var i = 0; i < masterlistData.length; i++) {
for (var j = 0; j < masterlistData[i].length; j++) {
if (masterlistData[i][j] == userId) {
var personalDatum = {
name: masterlistSheet.getRange(rowCounter, columnCounter + 1).getValue(),
period: masterlistSheet.getRange(rowCounter, columnCounter + 2).getValue(),
totalSales: masterlistSheet.getRange(rowCounter, columnCounter + 3).getValue(),
salesPerHour: masterlistSheet.getRange(rowCounter, columnCounter + 4).getValue()
};
<!-- Load the jQuery and jQuery UI libraries. -->
[removed][removed]
[removed][removed]
<!-- Custom client-side JavaScript code. -->
[removed]
// When the page loads.
$(function() {
$('#data_search').bind('submit', onSearchFormSubmit);
});
/**
* A callback function that runs when the data search form is submitted.
*/
function onSearchFormSubmit() {
loadPersonalData();
//loadTransactionData();
}
/**
* Load the personal data based on a particular user id
*/
function loadPersonalData() {
var userId = $('#user_id').val();
google.script.run.withSuccessHandler(showPersonalData)
.withFailureHandler(showError)
.getPersonalData(userId);
}
/**
* Show the returned personal data.
* @param {Array.<Object>} personalData The personal data to show.
*/
function showPersonalData(personalData) {
window.alert("Log: " + personalData.length);
if (personalData.length > 0) {
for (var i = 0; i < personalData.length; i++) {
window.alert("Arrived in this line of code. ");
var item = $('<li>');
.text(personalData[i].name);
list.append(item);
var period = $('<li>')
.text(personalData[i].period);
list.append(period);
var totalSales = $('<li>')
.text(personalData[i].totalSales);
list.append(totalSales);
var salesPerHour = $('<li>')
.text(personalData[i].salesPerHour);
list.append(salesPerHour);
}
} else {
var item = $('<li>: -</li><li>: -</li><li>: -</li><li>: -</li>');
list.append(item);
}
}
/**
* Logs an error message and shows an alert to the user.
*/
function showError(error) {
console.log(error);
window.alert('An error has occurred, please try again.');
}
[removed]