How to add to the results of delivery the images f

2019-09-14 15:39发布

问题:

I'm a beginner in programming . There is a script which returns the found range of values by highlighting the desired row. author @Jack Brown. I made small additions. Here is a script with which I'm working now. The question: how to add to the delivery of the text ("N1:V15") images from the range(“ W3:W17”)? that's the expected result Thank you!

function doGet(e)
    {
      var sheet = SpreadsheetApp.openById("1siy44rpMtdLmcCMUiopQ__Z13KkFnB_HMJhiJ_Qv0QY");
      sheet.getRange("A2").setValue(e.parameter.p1) ;
      var n= sheet.getRange("N1:V15").getValues();
      var h= sheet.getRange("L2").getValue(); //Line number for selection
      var z= sheet.getRange("M2").getValue(); 
      if (z==="#N/A"){return HtmlService.createHtmlOutput("Code not found");
                   }
    var retStr ="<style> .tcol { font: 10pt arial;font-weight: bold; color : DarkBlue; } </style> <table>"
      for(var i = 0 ; i< n.length ; i++){
       if ( i != h) {
        retStr +="<tr><td>"+ n[i].join("</td><td>") +"</td></tr>"
       } else {
        retStr +="<tr><td class = 'tcol'>" + n[i].join("</td><td class = 'tcol'>") +"</td></div></tr>"
       }

      }
      retStr += "</table>"

        return HtmlService.createHtmlOutput(retStr)
      }

Spreadsheet https://docs.google.com/spreadsheets/d/1siy44rpMtdLmcCMUiopQ__Z13KkFnB_HMJhiJ_Qv0QY/edit?usp=sharing

回答1:

Here's the example of a simple web app that I put together to demonstrate advanced templating in GAS. For simplicity, the spreadsheet used as a source of image urls contains a single column

1) Add html template to your GAS project. You create html files via File -> New -> Html file. I called the file 'images'.

2)In your doGet() function, retrieve the array of values from the spreadsheet, then attach it to your template object as property. This will allow you to reference the variable directly from html. Calling evaluate() on the template will execute inline code and build htmlOutput that gets sent to the browser.

function doGet(){

var ss = SpreadsheetApp.openById(yourId);
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:A" + sheet.getLastRow());
var urls = range.getValues();

var template = HtmlService.createTemplateFromFile('images');
template.urls = urls;

return template.evaluate();


}

3) For your html file, use scriptlets to dynamically create as many table cells as your array has values. More on templating here https://developers.google.com/apps-script/guides/html/templates

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <table>

    <tr><th>Images</th></tr>

    <?

    for(var i=0; i < urls.length; i++ ) { ?>

    <tr>

     <? for(var j=0; j < urls[i].length; j++) { ?>  

    <td> <img src="<?=urls[i][j]?>" /> </td>


    <?}?>

     </tr>
   <? }

    ?>

    </table>
  </body>
</html>

4) Deploy your script as a web app. The result is below