简体   繁体   中英

Google Apps Script modal window HTML - link's href issue

I am facing some strange issue with Google Apps Script modal window HTML parsing mechanism.

Minimal reproduceable example:

var ui = SpreadsheetApp.getUi();

var template = HtmlService.createTemplateFromFile('html/test');

var html = template.evaluate().setWidth(1920).setHeight(1080);
ui.showModalDialog(html, 'Test');

html/test.html :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a href="https://test.com/#gid=0&range=A1:B1" target="_blank">https://test.com/#gid=0&range=A1:B1</a>
</body>
</html>

Expected output in modal window: link that points to https://test.com/#gid=0&range=A1:B1 .

Actual link unexpectedly points to different URL: https://test.com/#gid=0%E2%A6%A5=A1:B1 .

Google Chrome developer tools shows next link's HTML:

<a href="https://test.com/#gid=0⦥=A1:B1" target="_blank">https://test.com/#gid=0&amp;range=A1:B1</a>

Why link's href gets overwritten? Is there anything can be done to make Google Apps Script display links as is?

Try it this way:

gs:

function displayMyDialog() {
  var ui = SpreadsheetApp.getUi();
  var template = HtmlService.createTemplateFromFile('ah3');//my html file name
  var html = template.evaluate().setWidth(1200).setHeight(450);//change to fit my window
  ui.showModalDialog(html, 'Test');
}

Replace & with &amp;

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a href="https://test.com/#gid=0&amp;range=A1:B1" target="_blank">https://test.com/#gid=0&amp;range=A1:B1</a>
</body>
</html>

honestly I can't remember where I learned this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM