无法更改image.src属性+函数执行但不返回值

时间:2016-02-25 07:48:37

标签: javascript google-apps-script google-sheets

请查看IndexQ3文件中的SPOT 1评论。这就是我遇到问题的地方。当用户点击“下一个问题”按钮时xxx()  执行并运行myFunc_3。后者有一些我无法检测的问题,即使它是一个5行函数。我知道它是通过在电子表格中输入新值来执行的。我知道它有问题,因为它返回" undefined"。

问题:我做错了什么?

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//
}

1 个答案:

答案 0 :(得分:0)

丹尼尔,谢谢!

如果其他人正在努力解决这个问题,那就是我所做的。在函数xxx()中,我添加了google.script.run.withSuccessHandler(onSuccess).myFunc_3()。然后在body script标签中添加。

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