简体   繁体   中英

How to Add variable from code.gs to Audio src?

Need help to fix this code please

I want to Add the value of my Variable to the src of my audio, so that the text to speech will read the value from my spreadsheet as text.

  function checkCalls() {
  
  google.script.run.withSuccessHandler(function (columnH) {
  var div = document.getElementById('call2');
  var newvar = getColumnH();
  div.src = "https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=" + newvar + "NEXT+CUSTOMER+PLEASE"
    for (var i = 0; i < columnH.length; i++) {

      // if there's a difference and it's a call, notify the user
      if (lastTime[i] !== columnH[i] && columnH[i] === "Call") {
        notify();
      }
    }

    // store results for next time
    lastTime = columnH;

    console.log(lastTime);

    // poll again in x miliseconds
    var x = 1000; // 1 second
    window.setTimeout(checkCalls, x);
  }).getColumnH();
}

newvar is from my code.gs

function getColumnH() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DASHBOARD");
// get the values in column H and turn the rows into a single values
var newvar = getColumnA();
console.log(newvar);
return sheet.getRange(1, 8, sheet.getLastRow(), 1).getValues().map(function (row) { return row[0]; 
});
}

function getColumnA(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("CONTROLS");
var r =  s.getRange("A1")
var lastvalue = r.getValues();
return lastvalue;
}

Div is not an audio element you should use Audio Element instead

By seeing your code var div = document.getElementById('call2');

The correct use of HTML5 Audio generated should be:

<audio controls>
  <source id="call2" src="added programatically" type="audio/mpeg">
</audio>

Or

<audio id="call2" src="added programatically" controls type="audio/mpeg"></audio>

If this is not the case and div means exactly as is, then you should change your HTML structure and make use of the above mentioned. Here's an example:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio

Otherwise the URL generated for the source is not correct.

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