this was a simple script I made. when the submit button is pressed the submitPointsClaimed function is called. The problem occurs when the submitPointsClaimed function runs, the inner function always returns null.
Cloud logs
Jul 9, 2024, 2:29:18 AM Info []
Does anyone know why this happened and how to fix this
function buildHtmlTable(userInfo, orderDetails, phoneNumber) {
var html = '<h2>User Information</h2>' +
'<table border="1" cellspacing="0" cellpadding="5">' +
'<tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Birth Date</th><th>Phone Number</th><th>Instagram Username</th></tr>' +
'<tr><td>' + userInfo.id + '</td>' +
'<td>' + userInfo.firstName + '</td>' +
'<td>' + userInfo.lastName + '</td>' +
'<td>' + userInfo.birthDate + '</td>' +
'<td>' + userInfo.phoneNumber + '</td>' +
'<td>' + userInfo.usernameIg + '</td></tr>' +
'</table><br>' +
'<h2>Order Details</h2>' +
'<table border="1" cellspacing="0" cellpadding="5">' +
'<tr><th>Order ID</th><th>Order Date</th><th>Spending</th><th>Point</th></tr>';
for (var orderId in orderDetails) {
var sumAfterDiscount = orderDetails[orderId].afterDiscountSum.toLocaleString();
var avgPointGained = orderDetails[orderId].avgPointGained.toFixed(0);
html += '<tr>' +
'<td>' + orderId + '</td>' +
'<td>' + orderDetails[orderId].orderDate + '</td>' +
'<td>' + sumAfterDiscount + '</td>' +
'<td>' + avgPointGained + '</td>' +
'</tr>';
}
html += '</table><br>' +
'<h2>Enter Points Claimed</h2>' +
'<input type="number" id="pointsClaimed" min="0" step="1" placeholder="Enter points claimed">' +
'<br><br><button id="point_button" onclick="submitPointsClaimed()">Submit Points Claimed</button>';
html += '<script>' +
'function submitPointsClaimed() {' +
' var points = document.getElementById("pointsClaimed").value;' +
' google.script.run.withSuccessHandler(() => { google.script.run.showUserInfo("' + phoneNumber + '"); }).submitPointsClaimed("' + userInfo.id + '", points);' +
'}' +
'</script>';
return html;
}
// search order id and its point
function searchOrderID(customer_id) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('order_detail');
const data = sheet.getDataRange().getValues();
const header = data[0];
const rows = data.slice(1);
const orderIdIndex = header.indexOf('id');
const customerIdIndex = header.indexOf('customer_id');
const pointIndex = header.indexOf('point_gained');
let temp = [];
rows.forEach((row, rowIndex) => {
if (row[customerIdIndex] === customer_id) {
temp.push({
order: rows[rowIndex][orderIdIndex],
point: rows[rowIndex][pointIndex]
});
}
});
let uniqueTemp = temp.filter((value, index, self) =>
index === self.findIndex((t) => (
t.order === value.order && t.point === value.point))
);
return uniqueTemp;
}
function subtractFromList(data, value) {
for (let i = 0; i < data.length; i++) {
data[i].point_claimed = 0;
if (data[i].point < value) {
value -= data[i].point;
data[i].point_claimed = data[i].point;
data[i].point = 0;
} else {
data[i].point_claimed = value;
data[i].point -= value;
value = 0;
}
}
return data;
}
function updatePoints(orderId, point, claimed) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('order_detail');
const data = sheet.getDataRange().getValues();
// Extract header and data separately
const header = data[0];
const rows = data.slice(1);
// Find relevant column indexes
const orderIdIndex = header.indexOf('id');
const pointGainedIndex = header.indexOf('point_gained');
const pointClaimedIndex = header.indexOf('point_claimed');
// Update rows where order_id matches
rows.forEach((row, rowIndex) => {
if (row[orderIdIndex] === orderId) {
const sumClaimed = (rows[rowIndex][pointClaimedIndex] || 0) + claimed;
sheet.getRange(rowIndex + 2, pointGainedIndex + 1).setValue(point);
sheet.getRange(rowIndex + 2, pointClaimedIndex + 1).setValue(sumClaimed);
}
});
}
function submitPointsClaimed(customerId, pointsToClaim) {
let subData = subtractFromList(searchOrderID(customerId), pointsToClaim);
Logger.log(subData);
for (let i = 0; i < subData.length; i++) {
updatePoints(subData[i].order, subData[i].point, subData[i].point_claimed)
}
}
when the submitPointsClaimed run, the inner function should be run successfully and return an array
Ahmad Syarif Pudin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.