简体   繁体   中英

Copy the content from a Sheets Cell to a Google Sites

I'm using the new Google Sites and I want to display a text box in it, but my goal is to show a text that is written in a cell in a Google sheet. So I can just edit the content in this sheet and it also change in the site.

I've written this code, but it is not working. Someone know what I can do?

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

function getText(row, col) {
  const ss = SpreadsheetApp.openById("here_goes_the_sheet_id");
  const sheet = ss.getSheetByName("Orientacoes");
  var row = row;
  var col = col;
  
  var value = sheet.getRange(row, col).getValue();
  return value; 
}


<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    
    <script>
       function copyTxt(2,1){
          var text = google.script.run.getText();
          console.log(text);
          document.getElementById("titulo").innerText = text;
       }
       
       copyTxt();
    </script>
  </head>
  <body>
    <div id="titulo">  </div>
  </body>
</html>

The code is using google.script.run the wrong way. From https://developers.google.com/apps-script/guides/html/reference/run

Return
void — this method is asynchronous and does not return directly; however, the server-side function can can return a value to the client as a parameter passed to a success handler; also, return types are subject to the same restrictions as parameter types, except that a form element is not a legal return type

Replace

function copyTxt(2,1){
   var text = google.script.run.getText();
   console.log(text);
   document.getElementById("titulo").innerText = text;
}

by

function copyTxt(){
   google.script.run.withSuccessHandler(function(text){
     console.log(text);
     document.getElementById("titulo").innerText = text;   
   })
   .getText(2,1);
   
}

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