I am trying to load a specific .html depending on the current cell's Text (either .value() or .getdisplayValue()).
However, for some reason only the Payroll.html file is ever being fired, even though the current cell's text is "Click To Upload Receipt"
function doGet(e) {
if (sheet.getActiveCell().getValue() === "Click To Upload Receipt") {
var htmlOutput = HtmlService.createTemplateFromFile('UploadFile');
} else {
var htmlOutput = HtmlService.createTemplateFromFile('Payroll');
}
htmlOutput.message = '';
return htmlOutput.evaluate();
}
I have ReDeployed my WebApp and set the URL of this as a link on the current cell.
Please let me know if you need any specific information to assist. Thank you all.
CodePudding user response:
An Apps Script webapp (container bound or not) has no concept of an "active cell," in the doGet
or doPost
event handler.
Instead, you should invoke the doGet
endpoint with a URL query parameter containing the desired template to display. You can access this from the event object received by the doGet
call.
For example,
https://scripts.google.com/a/.......?action=do payroll
This would invoke your doGet
handler with a function argument that has a property "parameter" with the property named "action" and the value "do payroll"
.
You could then load the desired parameter by inspecting this value, and provide a fallback if an unknown value is provided:
function doGet(eventObj) {
const action = eventObj.parameter.action;
if (action === "do payroll") {
// Do stuff
} else if (action === "do receipt") {
// Do other stuff
} else {
// Provide fallback for incorrect user input
}
}
This may be helpful: https://developers.google.com/apps-script/guides/web
CodePudding user response:
Despite that sheet
has not being defined, getActiveCell() doesn't work as you assumed for Google Apps Script web apps, but it might work for a dialog of sidebar.
The above because the web application instance hasn't the "active" context. If web app code belongs to a spreadsheet bounded project, SpreadsheetApp.getActiveSpreadsheet()
will return the bounded spreadsheet but methods like getActiveCell
and getActiveRange
will return A1 of the first sheet.
Instead you using the "active cell" you could include a query string (i.e. ?page=page_name
A very simplistic way to implement this:
function doGet(e){
const template = HtmlService.createTemplateFromFile(e.paramenter.page || 'index');
return template.evaluate();
}
Related
- Linking to another HTML page in Google Apps Script
- Multi-page site in Google Apps Script: How to invoke doGet() through jquery or javascript?
Resources