1
votes

I am trying to implement a workflow - I need to send an email with links to approve/reject a candidate to Level 1 Manager and then to Level 2 Manager. Once both approve, a confirmation email is sent to the candidate.

I have a custom function say main_function() that executes before sending each of the two emails. This function needs to pull data from the spreadsheet to which the script is bound.

Since I have a two-step approval, I created different projects to get separate WebApp URLs for each approval step.

I am including the main_function() function as a library in the two projects.

main_function() sends an email with approve/reject link and when the link is clicked an HTML opens with an input box to take comments. Then the HTML includes a call to a script function saveToSheets() to save the data to google sheet.

The HTML shows up but data is not getting saved because saveToSheets() is not called. How can I resolve this?

Main function in Library myLib

    main_function(){
    
    //do something
    Logger.log("function called!");
        
    var htmlTemplate = HtmlService.createTemplateFromFile('Index2.html'); 
    htmlTemplate.ID = ID; //pass variables from script to HTML
    htmlTemplate.decision = decision;
    htmlTemplate= htmlTemplate.evaluate().setTitle('Comments').setSandboxMode(HtmlService.SandboxMode.NATIVE); 
    
    return htmlTemplate.asTemplate(); 
        
    }

saveToSheets(inputArray){
//do something
}

Index2.html in Library myLib

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>

</style>


</head>
<body>

// Includes a COMMENT BOX with id comment1
//include DIV element with id output to catch error
<script>  

function runGoogleScript() {

var item0 = "<?= ID ?>";
var item1 = "<?= decision ?>";
var item2 = document.getElementById('comment1').value; //comments

var inputArray =[item0,item1,item2];

google.script.run.withFailureHandler(onFailure).myLib.saveToSheets(inputArray);

}



function onFailure(error) {
        var div = document.getElementById('output');
        div.innerHTML = "ERROR: " + error.message;
      }



function onSuccess() {

}


</script>

</body>
</html>

Function in another project that needs to reuse the script and HTML through mylib

myfunction(){
     
    var htmlTemplate = mylib.main_function();
    return htmlTemplate.evaluate();  
      
    }
1
Welcome to SO! Your question is about software design at this stage. Not a programming question. Please ask this question in softwareengineering.stackexchange.com - Aerials
@Aerials when referring other sites, it is often helpful to point that cross-posting is frowned upon - gnat
I am confused, can I get my question answered here? I would prefer to not post it on another site as I don't want to register on a new site. This is specific to the google apps script programming features, so in my opinion, it is a programming question. - newToScripts
Now that you added code yes! - Aerials

1 Answers

1
votes

It is incorrect to say "Index.html is not accessible from project". When you deploy a project as a library, its HTML files are in the context of the library.

But if you want to pass an evaluated HTML template from a library as a template you should use asTemplate()

Example:

main_function(){
//do something
Logger.log("function called!");

var htmlTemplate = HtmlService.createTemplateFromFile('Index.html');
    htmlTemplate= htmlTemplate.evaluate();
    return htmlTemplate.asTemplate();

}