LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Friday, May 1, 2020

speechSynthesis.getVoices()で取得した中から特定のvoiceのindexを取得したい


セレクトボックス内の値をクリックした時にそのindexをアラート表示する。

デモ




コード.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}
意訳
この機能がやること
指定したHTMLファイルを表示する




index.html
<!DOCTYPE html>
<html>
<body>
<select id="voiceSelect"></select>

<script>
document.getElementById("voiceSelect").onclick = getVoiceIndex

speechSynthesis.onvoiceschanged = getVoices;

var voices;
function getVoices() {
  voices = speechSynthesis.getVoices();
  var select = document.getElementById("voiceSelect");
  var values = "";
  for(var i = 0; i < voices.length ; i++) {
    var option = document.createElement('option');
    var lang = voices[i].lang;
    var value = voices[i].name
    option.textContent = value;
    option.setAttribute('data-lang', voices[i].lang);
    option.setAttribute('data-name', voices[i].name);
    select.appendChild(option);
  }
  select.setAttribute('size', 12);
}

function getVoiceIndex() {
  var name = document.getElementById("voiceSelect").value;
  var voiceIndex;
  for(var i = 0; i < voices.length; i++) {
    if(voices[i]["name"] === name) {
      voiceIndex = i;
      break;
    }
  }
  alert(voiceIndex);
}

</script>
</body>
</html>



Latest post

スプレッドシートA列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...