To update your HTML and JavaScript code so that column B remains fixed at B1 for input while also allowing users to view and interact with your table, you can follow these steps:
Step 1: Modify the HTML Structure
You need to add a section in your HTML file to display the fixed value of Column B. Assuming you want to get the value from your Google Sheets, you'll display it above the table. Below is the modified Index.html file:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('Css'); ?>
</head>
<body>
<div class="container">
<h4 class="text-center">ช่วยฉันด้วย!!!!</h4>
<div class="fixed-b-container">
<label class="input-label">Column B (fixed): </label>
<div id="fixedB">[Fixed value here]</div> <!-- This will display the fixed value -->
</div>
<div id="example-table"></div>
</div>
<?!= include('JavaScript'); ?>
</body>
</html>
Step 2: Update the JavaScript to Display the Fixed Value
You need to modify the JavaScript section to fetch and display the fixed value of Column B. You can pass this value from the doGet() function in your Google Apps Script.
Here's the updated JavaScript section:
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@5.6.1/dist/js/tabulator.min.js"></script>
<script>
// Trigger table creation when the DOM is ready.
document.addEventListener("DOMContentLoaded", createTable);
function createTable() {
google.script.run.withSuccessHandler((response) => {
const { tableData, fixedB } = response; // Get the table data and fixed value
console.log(tableData);
// Set the fixed value in the HTML
document.getElementById("fixedB").innerText = fixedB;
// Initialize the Tabulator table
let table = new Tabulator("#example-table", {
data: tableData,
pagination: false,
paginationSize: 1000,
paginationSizeSelector: false,
movableColumns: true,
responsiveLayout: "collapse",
layout: "fitColumns",
initialSort: [{ column: "Topic", dir: "asc" }],
columnDefaults: { tooltip: false },
columns: [
{
title: "Topic",
field: "Topic",
headerFilter: true,
headerFilterFunc: wordFilter,
formatter: function (cell) {
return `<div class="wrap-text">${cell.getValue().replace(/\n/g, '<br>')}</div>`;
},
},
{ title: "Note", field: "Note", editor: "input" },
],
});
}).getData();
}
// Custom filter function to filter by words in the "Topic" column
function wordFilter(headerValue, rowValue) {
if (!headerValue) {
return true; // If no input, show all rows
}
const words = headerValue.toLowerCase().split(/\s+/);
const cellValue = rowValue.toString().toLowerCase();
return words.every(word => cellValue.includes(word));
}
</script>
Step 3: Update the getData Function in Google Apps Script
You need to modify your getData() function in Google Apps Script to return both the table data and the fixed value from Column B:
function getData() {
const range = Sheets.Spreadsheets.Values.get(SPREADSHEET_ID, DATA_RANGE);
const data = range.values;
if (!data || data.length === 0) {
return {
tableData: [],
fixedB: "No data available."
};
}
// Extract headers
const headers = data.shift(); // Remove the first row and use it as headers
// Get the fixed value for Column B (index 1)
const fixedB = headers[1]; // Replace this with the actual fixed value if needed
// Format the rest of the data as objects
const tableData = data.map(row => {
const obj = {};
for (let i = 0; i < headers.length; i++) {
obj[headers[i]] = row[i];
}
return obj;
});
Logger.log(tableData);
return {
tableData: tableData,
fixedB: fixedB // Return fixed value
};
}
Summary of Updates
- In the HTML, a div for displaying the fixed value of Column B is added.
- In the JavaScript, when receiving the data from Google Apps Script, it updates the HTML accordingly.
- In the
getData function on the server side, both the table data and fixed value are returned.
With these modifications, you should see the fixed value from column B displayed prominently on top of your table, and users can input data as required while interacting with the Tabulator table.