0
votes

I have a PHP form where users enter some data. The form then pulls additional data from the backend database and submits it all to a Google Apps Script. Amongst other things, the Google script creates and auto-populates a new Google Sheet. I would like the script to return some HTML so that the user can click on the link and access the Google sheet. However, whenever I try and generate the HTML, I always get left with a blank box:

enter image description here

Here is the relevant code from the Google script:

var htmlLinkToFile = "<p>The form is ready and can be accessed here: <a href=\"" + newFileUrl + "\">" + newFileUrl + "</a></p>";

var output = HtmlService.createHtmlOutput();
output.setContent(htmlLinkToFile);

return HtmlService.createHtmlOutput(output);

I have tried this version, and omitting the middle two lines and directly returning htmlLinkToFile but I keep getting the same result. If I just return the content using

return ContentService.createTextOutput(newFileUrl);

I can view the URL to the file, but obviously I would like to be able to click through.

In case it matters, here is the relevant PHP code as well:

$url = curl_init("https://script.google.com/macros/s/MYSCRIPT/exec");

curl_setopt($url, CURLOPT_POST, 1);
curl_setopt($url, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($url, CURLOPT_FOLLOWLOCATION, 1);

curl_exec($url);
curl_close($url);

Grateful for any advice.

1

1 Answers

0
votes

I have come up with a workaround which is to return the link (htmlLinkToFile variable) as a text output in the Google script:

return ContentService.createTextOutput(newFileUrl);

And then make the following changes to the PHP (see comments at end of line):

$url = curl_init("https://script.google.com/macros/s/MYSCRIPT/exec");

curl_setopt($url, CURLOPT_POST, 1);
curl_setopt($url, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($url, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($url, CURLOPT_RETURNTRANSFER, 1); //this line added

echo curl_exec($url); //echo added
curl_close($url);

However, it would still be nice to understand why I was originally getting a blank box.