If I understood your query correctly, instead of stopping the loop when a match to "Total" is found, perform necessary actions within the loop as shown below:
var today = toDateFormat(new Date());
var todaysColumn =
values[5].map(toDateFormat).map(Number).indexOf(+today);
var emailDate = Utilities.formatDate(new Date(today),"GMT+1",
"dd/MM/yyyy");
for (var i=0; i<values.length; i++){
if (values[i][0]=='Total'){
nr = i;
Logger.log(nr);
var output = values[nr][todaysColumn];
// Deal with the output here - assuming it will be emailed
}
}
The loop will continue and find every instance of "Total" to execute the same action. This approach assumes that the "Totals" are in the same column. You can customize this further if you only want specific tables to trigger an action, but this should give you a starting point.
I may have not fully grasped the second part of your question...
"Also, is there any solution to automatically find the array number which contains the date row for each table (instead of defining this manually)? Hope this explanation makes sense."
I presume you wish to identify all rows containing "Total" in a particular column. You could initialize a variable as an empty array like so, var totals = [];
. Then, rather than executing the action in the initial loop, push the row values to the array using
totals.push(nr+1) . //adding 1 gives you the actual row number (rows count from 1 but arrays count from 0)
. Subsequently looping through the totals array would enable you to perform desired actions. Alternatively, you could create an array of all the values instead of row numbers like
totals.push(values[nr][todaysColumn])
and iterate over that array. There are numerous methods to tackle this issue!
Based on our previous discussion, I have made adjustments to the "test" sheet and updated the code. Below are my revisions
All modifications have been applied in your test sheet and confirmed functional in Logger. Feel free to reach out if you have any queries.
Spreadsheet:
- Added "Validation" Tab
- Adjusted "Table" tab so that the row with "Email Address" in Column A aligns with the desired lookup values (dates or categories)...this was specifically done for the first two tables as others already met this criteria.
Code:
Create table/category selector...
- In the editor navigate to File >> New >> HTMLfile
- Name the file "inputHTML"
- Paste the provided code into that file
<!DOCTYPE html> ... [Code truncated for brevity]
Edits to Code.gs file
Replace existing code in Code.gs with the revised version...
//This simple trigger generates the menu item in your sheet
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Run Scripts Manually')
.addItem('Get Total','fncOpenMyDialog')
.addToUi();
}
//Function to open the dialog triggered by the menu item
function fncOpenMyDialog() {
//Launch a dialog
var htmlDlg = HtmlService.createHtmlOutputFromFile('inputHTML')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(200)
.setHeight(150);
SpreadsheetApp.getUi()
.showModalDialog(htmlDlg, 'Select table to get total for');
};
//Main function triggered by clicking "Get Total" in the dialogue...variables passed from formSubmit in inputHTML javascript
function getTotal(table,catagory) { ... [Code truncated for brevity]
/** Functions below populate selects used by the form **/
function getTables(){ ... [Code truncated for brevity]
function getCatagories(){ ... [Code truncated for brevity]