1

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();  
      
    }
4
  • Welcome to SO! Your question is about software design at this stage. Not a programming question. Please ask this question in softwareengineering.stackexchange.com Commented Mar 2, 2021 at 14:52
  • 1
    @Aerials when referring other sites, it is often helpful to point that cross-posting is frowned upon Commented Mar 2, 2021 at 20:03
  • 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. Commented Mar 2, 2021 at 21:16
  • Now that you added code yes! Commented Mar 3, 2021 at 12:10

1 Answer 1

1

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();

}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this works for a simple HTML. Now if I have an HTML in library to which I pass parameter, then collect user's comment in HTML input box, save it to sheets through a call from HTML to script function, it doesn't save the date. I have included the code in EDIT 2 above.
* I meant it doesn't save the data
What's in your "Index2.html"? And does the line google.script.run.withFailureHandler(onFailure).myLib.saveToSheets(inputArray); ever work ?
The line you mentioned above was not working i.e. function saveToSheets was not being called from the project using the library myLib. HOWEVER, it started working without even the need for myLib qualifier i.e. the following code works now AS LONG AS I have a same name, empty function in projects using this library google.script.run.withFailureHandler(onFailure).saveToSheets(inputArray); Do you know why this happens and if this is the ONLY solution? (Index2 is the html I have included in my question. I have corrected the name now.)
Also, how do test the withFailureHandler? At present, I don't see it being called.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.