0

I found this example here on Stack Overflow which is exactly what I was looking for:

https://stackoverflow.com/a/40013027/1955916

I tried several approaches to include this code in to Google Sites. I tried with an HTML gadget, directly as HTML and as JavaScript gadget. But so far nothing worked.

Based on the example of user Grant: document.getelementbyId in Google Script not working I think I am on a promising way, but it still does not work.

I have stored the following script in Google sites and published it as a web app. Then I included it as a Apps Script Gadget in my testpage. However, the only thing that is displayed is the Title. So I must have a fundamental error somewhere, but my current knowledge is really limited here.

Code.gs:

function doGet(e) {
 return HtmlService.createHtmlOutputFromFile('Index'); 
}

Index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Demo of a canvas used to render seat plan in a Cinema</title>
        <script>
            var EMPTY = 0; // Still available for reservation and purchase.
            var RESERVED = 1; // reserved but not yet paid for.
            var BOUGHT = 2; // bought and paid for.

            function Point(x,y) {
                return { X: x, Y: y }
            }
            function Size(w,h) {
                return {Width: w, Height: h}
            }
            function Rectangle(left,top,width,height) {
                return {TopLeft: Point(left,top), Size: Size(width,height)}
            }
            function seatColorFromSeatStatus(seatStatus) {
                switch(seatStatus) {
                    case EMPTY: return "white";
                    case RESERVED: return "green";
                    case BOUGHT: return "red";
                    default: return "black"; // Invalid value...
                }
            }
            function mapSeatStatusToSeatColor(seats)
            {
                var result = {};
                for(seat in seats) {
                    result[seat] = seatColorFromSeatStatus(seats[seat])
                }
                return result;
            }
            function seatKeyFromPosition(row,col) {
                return JSON.stringify([row,col]);
            }
            function seatRowFromKey(key) {
                return (JSON.parse(key))[0];
            }
            function seatColFromKey(key) {
                return (JSON.parse(key)[1]);
            }
            function getSeatInfo(nrows,ncolumns) {
                var result = { NRows: nrows, NColumns: ncolumns, Seats : {} };
                for(row = 0; row < nrows; row++) {
                    for( col = 0; col < ncolumns; col++ ) {
                        result.Seats[seatKeyFromPosition(row,col)] = EMPTY;
                    }
                }
                result.Seats[seatKeyFromPosition(0,0)] = RESERVED;
                result.Seats[seatKeyFromPosition(1,3)] = BOUGHT;
                return result;
            }
            function renderSeat(ctx,r,fillColor) {
                var backup = ctx.fillStyle;
                ctx.strokeStyle = "blue";
                ctx.rect(r.TopLeft.X+2,r.TopLeft.Y+2,r.Size.Width-4,r.Size.Height-4);
                ctx.stroke();
                ctx.fillStyle = fillColor;
                ctx.fillRect(r.TopLeft.X+3,r.TopLeft.Y+3,r.Size.Width-5,r.Size.Height-5);
                ctx.fillStyle = backup;
            }
            function renderSeatplan(seatInfo) {
                var nrows = seatInfo.NRows;
                var ncolumns = seatInfo.NColumns;
                var seatColors = mapSeatStatusToSeatColor(seatInfo.Seats)
                var canvas = document.getElementById("seatplan");
                var ctx = canvas.getContext("2d");

                var borderWidth = 10;
                var rcContent = Rectangle(
                    borderWidth
                    , borderWidth
                    , canvas.width - 2 * borderWidth
                    , canvas.height - 2 * borderWidth
                );
                var szCell = Size(
                    Math.floor(rcContent.Size.Width / (ncolumns + 1))
                    , Math.floor(rcContent.Size.Height / (nrows + 1))
                );
                ctx.font = "30px Arial";

                for(row = -1; row < nrows; row++) {
                    for(col = -1; col < ncolumns; col++ ) {
                        var r = Rectangle(
                            rcContent.TopLeft.X + szCell.Width * (col+1)
                            ,rcContent.TopLeft.Y + szCell.Height * (row+1)
                            ,szCell.Width
                            ,szCell.Height
                            );
                        var center = Point(szCell.Width / 2, szCell.Height / 2);
                        if (row == -1 && col == -1) {
                            // nothing to render.
                        }
                        else if(row == -1){
                            // render column headers as numbers...
                            ctx.fillStyle = "black";
                            ctx.textAlign = "center";
                            ctx.fillText(col.toString(),r.TopLeft.X+center.X,r.TopLeft.Y+center.Y+6);
                        }
                        else if(col == -1){
                            // render row header
                            ctx.fillStyle = "black";
                            ctx.textAlign = "center";
                            ctx.fillText(String.fromCharCode(65 + row),r.TopLeft.X+center.X+4,r.TopLeft.Y+center.Y+6);
                        }
                        else
                        {
                            // render seat
                            renderSeat(ctx,r,seatColors[seatKeyFromPosition(row,col)]);
                        }
                    }
                }
            }
        </script>
    </head>
    <body>
      <form id="myForm" onsubmit="renderSeatplan(getSeatInfo(10,16));">
      </form>
      <div id="output"></div>
      <h1>Seatplan</h1>
      <canvas id="seatplan" width="640" height="480"></canvas>
    </body>
</html>

The code of the Index.html on its own is proven, as it works in the example from user BitTrickler (see link above) I have seen.

Heinz Ruffieux
  • 41
  • 1
  • 1
  • 8
  • You need to use an Apps Script Gadget. – Alan Wells Dec 03 '17 at 20:41
  • What do you mean by "Javascript gadget"? – Rubén Dec 03 '17 at 21:06
  • Thanks for your comments Sandy and Rubén. My appologies, I meant a Apps Script Gadget. This is what I am using now, but somehow none of the functions in the script of Index.html do not return any values on the page. Only the title "Seatplan" is shown. – Heinz Ruffieux Dec 03 '17 at 22:49
  • If you add a button like this `` and press it you'll seat your seat plan. – Cooper Dec 04 '17 at 01:05
  • Actually just add this to your script tag: `$(function(){ renderSeatplan(getSeatInfo(10,16)); });` and you need to add this to the head somewhere `` It will display the seat plan as soon as the dom is loaded. – Cooper Dec 04 '17 at 01:11
  • Thank you very much for your support Cooper! Wow, what a superb service! I successfully implemented the button solution. The second one however, which would be preferrable, I could not yet get to work: I added the "ajax" line to line 4 in my inde.html file above just below the tag and the "function" line 1:1 from your example just above the tag in the example above. Is that the right place? Do I need to add some parameters? I tried several approaches based on other examples, but the seat plan does not show up. – Heinz Ruffieux Dec 05 '17 at 08:08

0 Answers0