0

function doGet() {
  return HtmlService.createHtmlOutputFromFile("vi");
  // var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
  
}
function doPost() {
  return HtmlService.createHtmlOutputFromFile("vi");
  // var output = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
  
}




// function from https://stackoverflow.com/a/9733420/3695983                     
function luminance(r, g, b) {
  var a = [r, g, b].map(function (v) {
    v /= 255;
    return v <= 0.03928
      ? v / 12.92
    : Math.pow( (v + 0.055) / 1.055, 2.4 );
  });
  return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
}

function calculateRatio(color1, color2) {

  // read the colors and transform them into rgb format
  // const color1 = document.querySelector("#color-1").value;
  // const color2 = document.querySelector("#color-2").value;
  const color1rgb = hexToRgb(color1);
  const color2rgb = hexToRgb(color2);

  // calculate the relative luminance
  const color1luminance = luminance(color1rgb.r, color1rgb.g, color1rgb.b);
  const color2luminance = luminance(color2rgb.r, color2rgb.g, color2rgb.b);

  // calculate the color contrast ratio
  const ratio = color1luminance > color2luminance 
  ? ((color2luminance + 0.05) / (color1luminance + 0.05))
  : ((color1luminance + 0.05) / (color2luminance + 0.05));
  

  return ratio;
  // Logger.log(ratio);
}
function hexToRgb(hex) {
  
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

// document.querySelector("btn").addEventListener("click", function() {
//   const ratio = calculateRatio();
//   // show results depending on WCAG requirements
//   const result = `
// AA-level large text: ${ratio < 1/3 ? 'PASS' : 'FAIL' }<br>
// AA-level small text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
// AAA-level large text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
// AAA-level small text: ${ratio < 1/7 ? 'PASS' : 'FAIL' }
// `;
//   document.querySelector("#result").innerHTML = result;
// });
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <label>Foreground Color:</label>  <input type="text" id="color-1"  />
    <label2>BackGround Color:</label2><input type="text" id="color-2"  />

<button id="btn">Calculate Color Contrast</button>

<div id="#result">
  <label>Result : </label>
</div>

<script>

document.getElementById("btn").addEventListener("click",doStuff);
function doStuff(){
  var fcolor = document.getElementById("color-1").value;
  var bgcolor = document.getElementById("color-2").value;

  google.script.run.withSuccessHandler(ratio => {
    const result = `
    AA-level large text: ${ratio < 1/3 ? 'PASS' : 'FAIL' }<br>
    AA-level small text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
    AAA-level large text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
    AAA-level small text: ${ratio < 1/7 ? 'PASS' : 'FAIL' }`;
    document.getElementById("#result").innerHTML = result;
  }).calculateRatio(fcolor, bgcolor);
}



// document.getElementById("btn").addEventListener("click", function() {
//   const ratio = calculateRatio();
//   // show results depending on WCAG requirements
//   const result = `
// AA-level large text: ${ratio < 1/3 ? 'PASS' : 'FAIL' }<br>
// AA-level small text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
// AAA-level large text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
// AAA-level small text: ${ratio < 1/7 ? 'PASS' : 'FAIL' }
// `;
//   document.querySelector("#result").innerHTML = result;
// });
</script> 
  </body>
</html>

I am trying to make a google script web app that takes input from an HTML form and passes the input from script. Right now, the function is failing because the resulted value is not getting printed in html page. How can I fix this?

However when I run this from the deployment, I am not getting the expected output. When I am entering the values and clicking on button, nothing happening. The same is working with javascript though. Can someone help me in using this code in Google AppScript? that how can I print the resulted output with the help of AppScript?

kanu priya
  • 98
  • 11
  • I think the issue here stems from your Apps Script functions and the values you're inputting in your web app. Have you tested the functions separately? Moreover, have you *redeployed* the web app after making any changes? – ale13 Jan 14 '21 at 12:20
  • Yes I do tested the functions separately by passing value of colors and they r working as expected . And yes I do redeployed the web app as well @ale13 . – kanu priya Jan 15 '21 at 03:00
  • @ale13 yes it is working with new deployement – kanu priya Jan 15 '21 at 04:03

1 Answers1

1

Modification points:

  • When you want to run the Google Apps Script from HTML side, please use google.script.run. When I saw your script, it seems that google.script.run is used as a comment line. In this case, in order to use the returned value from Google Apps Script, withSuccessHandler is used.

When above points are reflected to your script, it becomes as follows.

Modified script:

Please modify doStuff() as follows.

function doStuff(){
  var fcolor = document.getElementById("color-1").value;
  var bgcolor = document.getElementById("color-2").value;

  google.script.run.withSuccessHandler(ratio => {
    const result = `
    AA-level large text: ${ratio < 1/3 ? 'PASS' : 'FAIL' }<br>
    AA-level small text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
    AAA-level large text: ${ratio < 1/4.5 ? 'PASS' : 'FAIL' }<br>
    AAA-level small text: ${ratio < 1/7 ? 'PASS' : 'FAIL' }`;
    document.getElementById("#result").innerHTML = result;
  }).calculateRatio(fcolor, bgcolor);
}

Note:

  • In this case, your HTML&Javascript is required to be included in Google Apps Script project. Please be careful this.
  • In this modification, it supposes that your calculateRatio of Google Apps Script works fine and the correct values are returned.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thankyou for your valuable input, however it is still not returning anything on webapp. @Tanaike – kanu priya Jan 14 '21 at 09:32
  • please reply @tanaike – kanu priya Jan 14 '21 at 11:47
  • 1
    @kanu priya Thank you for replying. About `please reply`, I deeply apologize for my late response. This is due to my situation. About your issue, unfortunately, I cannot replicate it. When I tested it, the values from `calculateRatio` in GAS side are shown at the browser. I apologize for this. So, for example, when you redeployed your Web Apps as new version and test it again? Because the latest script is reflected by redeploying the Web Apps as new version. I'm worry about this. – Tanaike Jan 14 '21 at 12:14
  • @kanu priya Even when you redeployed the Web Apps as new version, can you provide your whole script for replicating your issue? If you can do, please add it to your question. By this, I would like to confirm it. – Tanaike Jan 14 '21 at 12:27
  • Sure @tanaike, adding latest script to my question. And yes I redeployed the Web App , however not getting expected response – kanu priya Jan 15 '21 at 02:53
  • @kanu priya Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand about `however not getting expected response`. Your 1st replying, you said `however it is still not returning anything on webapp`. In your current stage, that is different from your 1st replying? Because in your current script, from `const color1rgb = hexToRgb("#ffffff")` and `const color2rgb = hexToRgb("111222")`, `calculateRatio` returns `0.05397406096975492` as `ratio`. – Tanaike Jan 15 '21 at 03:28
  • @kanu priya And, under this situation, when the button is clicked on Web Apps, the value of `AA-level large text: PASS AA-level small text: PASS AAA-level large text: PASS AAA-level small text: PASS` is displayed. If this value is `however not getting expected response`, I think that this is different from your 1st replying. How about this? So I cannot understand about your goal. Can I ask you about the detail of your goal? By this, I would like to confirm it. – Tanaike Jan 15 '21 at 03:28
  • sorry i hardcoded the values in above code editing it again, – kanu priya Jan 15 '21 at 04:03
  • and yes it is working now after deploying it with New deployement :) thankyou so much @tanaike – kanu priya Jan 15 '21 at 04:03
  • @kanu priya Thank you for replying and testing it again. I'm glad your issue was resolved. – Tanaike Jan 15 '21 at 05:08
  • can you please help me in resolving this query as well? @tanaike https://stackoverflow.com/questions/65732708/translate-javascript-to-google-appscript – kanu priya Jan 15 '21 at 08:24