2016-02-25 87 views
0

請查看IndexQ3文件中的SPOT 1條評論。這是我遇到問題的地方。當用戶點擊下一個問題按鈕xxx() 被執行並且myFunc_3被運行。後者有一些問題,即使它是5線功能,我也無法檢測到。我知道它是通過在電子表格中輸入新值來執行的。我知道它有什麼問題,因爲它返回「未定義」。無法更改image.src屬性+函數執行但不返回值

問題:我在做什麼錯?

IndexQ3文件:

<!DOCTYPE html> 
<html> 
    <head> 
    <base target="_top"> 
    </head> 
    <body> 
     <script> 
      function saveResponse() {return document.getElementById('text').value;} 
      function returnText() {return "New Text!"} //function that changes the xx element 
      //function returnSrc() {var aaaa = '"' + returnCellValue(6) + '"'} //this approach is not working 

      function xxx() //function that is run when user clics button Next Question 
      { 
      document.getElementById("xx").innerHTML = returnText(); //Works 

      //SPOT 1 
      //the next line of code works partly - the myFunc_3() does get and write the value correctly 
      //but the variable uu is undefined despite "return b" 
      var uu = google.script.run.myFunc_3(); // 
      if(typeof uu == typeof "asdf") {uu = "1"} else {uu = "2"} //src value will be changed to 2 because of undefined 

      //document.getElementById("img").src = "https://www.google.com/images/srpr/logo3w.png" 
      document.getElementById("img").src = uu; //the scr value comes out as undefined 
      } 

     </script> 
    <div style="width: 100%; display: table;"> 
    <div style="display: table-row"> 
     <div style="display: table-cell;"> <!-- width: 400px --> 
      <p id="xx"> Question:</p>  
      <br> 
      <p style="font-size: 14pt"><?!= returnCellValue(2) ?></p> 
      <br> 
      <textarea id="text" rows="10" cols="30" style="font-size: 14pt">Enter the response here..</textarea> 
      <br> 
      <input type="button" value="Save Response" onclick='google.script.run.myFunc_3(saveResponse())' /> 
      <input type="button" value="Next Question" onclick='google.script.run.nextQuestion(); xxx()' /> 
      </div>  
     <div style="display: table-cell;"> 
      <br> 
      <img id="img" src="<?!= returnCellValue(6) ?>" alt="No Image" width="650"> 
      </div> 
    </div> 
</div> 
    </body> 
</html> 

CodeQ3.gs文件

function returnCellValue(ColumnNo) { 
//var ColumnNo = 6;// the data column number 
var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var ssh = ss.getActiveSheet(); 
var rowInd = ssh.getActiveCell().getRowIndex(); //.getRange(cell).getValue(); 
var ret = ssh.getRange(rowInd, ColumnNo).getValue(); 
//return ssh.getRange(rowInd, ColumnNo).getValue(); 
return ret; 
} 


//Opens the dialog box 
function openDialog() { 
    var html = HtmlService.createTemplateFromFile('IndexQ3') 
     .evaluate() 
     .setSandboxMode(HtmlService.SandboxMode.IFRAME).setHeight(700).setWidth(1000); 

    SpreadsheetApp.getUi().showModalDialog(html, 'Click here to drag'); 
} 

function nextQuestion_3() { 
    ssh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    var row = ssh.getActiveRange().getRowIndex() + 1; 
    ssh.setActiveSelection("A"+row); 
    //document.getElementById('img2').src = returnCellValue(6); 
} 


function myFunc_3(){ 
var b = "https://www.google.com/images/srpr/logo3w.png"; 
    ssh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    var row = ssh.getActiveRange().getRowIndex(); 
    var vs = ssh.getRange(row+1, 6).getValue(); 
    ssh.getRange(row, 3).setValue(b)//vs 
    return b//somevalue// 
} 
+0

「[功能是]異步並且不直接返回;然而,[它]可以將值返回給客戶端傳遞給一個_success handler_的參數」見https://developers.google.com /功能/運行#withSuccessHandler(功能) – daniel

+0

丹尼爾,我不知道如何標記你的評論作爲最好的答案,所以我們走吧:這是最好的答案! – Roy

回答

0

丹尼爾,謝謝!

如果其他人正在爲此苦苦掙扎,這就是我所做的。在函數xxx()中我添加了google.script.run.withSuccessHandler(onSuccess).myFunc_3()。然後在body script標籤中添加。

function onSuccess(sss) { 
      var uu = sss; 
      document.getElementById("img").src = sss; 
      }