3

私は初心者のコーダーであり、Google シート クエリを高速化するための助けが必要です。私はたくさん見回しましたが、解決策を見つけることができません。Googleシートに25個のクエリをすばやく実行しようとしていますが、これが私が試している方法です:

  1. Google シートに対してクエリを実行します。
  2. 結果を配列にプッシュします。
  3. 別のクエリを実行します。
  4. 結果を同じ配列にプッシュします。
  5. 次のクエリを実行します... x 25。
  6. 最終結果の表を描画します。

問題は、クワイアを 1 つずつ実行すると、完了までに 25 秒かかることです。for ループでクワイアを実行すると、結果は 2 秒で返されますが、非同期であり、データはすべておかしくなりません。おそらく、promise などを使用する必要があります。助けてください。

遅いバージョンのコードは次のとおりです。

   var resultData;
var allData = [];
var i = 0;

function startQuery() {
  i = 0;
  allData.length = 0;
  runOnecode();
}

function runOnecode() {
  var str  = "";
  str += $(".google-visualization-controls-rangefilter-thumblabel").text();
  var until = str.substring(10);
  var from = str.substring(0,10);
  var center = $("#centerSelect option:selected").text();
  var query = [
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND G =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND H =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND I =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND J =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND K =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND N =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND O =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND P =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND Q =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND R =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND S =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND T =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND U =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND V =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND W =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND X =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND Y =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND Z =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AA =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AB =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AC =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AD =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AE =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AF =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AG =\"No\" ")
  ];

  getData(query[i]);
}
</script>
<script>
function getData(incomingQuery) {

  // Prepare the query 
  var queryString = encodeURIComponent(incomingQuery);
  var query = new google.visualization.Query(
    //Collect the data from this spreadsheet
  'MySpreadSheet URL' + queryString);
  query.send(handleSampleDataQueryResponse);

  //Handle any errors from the Google server
  function handleSampleDataQueryResponse(response) {
    if (response.isError()) {
      alert('Error in query: ' + response.getMessage() + ' ' +
        response.getDetailedMessage());
      return;
    }

    // get look into the resulting data and find the detail.     
    var datas = response.getDataTable();
    if (datas.Gf.length === 0) {
      resultData = 0;
    } else resultData = datas.Gf[0].c[0].v;

    allData.push(resultData);
    //     console.log(allData);

    if (i < 25) {
      runOnecode();

    } else {
      drawTable();
    }
    i++;
  }

}
</script>

結果を非同期で返す高速バージョンを次に示します。

<script>
function runOnecode() {
  var str  = "";
  str += $(".google-visualization-controls-rangefilter-thumblabel").text();
  var until = str.substring(10);
  var from = str.substring(0,10);
  var center = $("#centerSelect option:selected").text();
  var query = [
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND G =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND H =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND I =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND J =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND K =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND N =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND O =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND P =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND Q =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND R =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND S =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND T =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND U =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND V =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND W =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND X =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND Y =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND Z =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AA =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AB =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AC =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AD =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AE =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AF =\"No\" "),
    ("SELECT COUNT(A) WHERE C =\"" + center + "\" AND todate(A)  >=date \'" + from + "\' AND todate(A)  <=date \'" + until + "\' AND AG =\"No\" ")
  ];
for(var i=0; i<25; i++){
  getData(query[i]);
// When all are done I will draw the table. 

}
}
</script>
<script>
function getData(incomingQuery) {

  // Prepare the query 
  var queryString = encodeURIComponent(incomingQuery);
  var query = new google.visualization.Query(
    //Collect the data from this spreadsheet
    'MySpreadSheet URL' + queryString);
  query.send(handleSampleDataQueryResponse);

  //Handle any errors from the Google server
  function handleSampleDataQueryResponse(response) {
  if (response.isError()) {
      alert('Error in query: ' + response.getMessage() + ' ' +
       response.getDetailedMessage());
      return;
    }

    // get look into the resulting data and find the detail.     
    var datas = response.getDataTable();
    console.log(datas);
    if (datas.Gf.length === 0) {
      resultData = 0;
    } else resultData = datas.Gf[0].c[0].v;

    allData.push(resultData);
      console.log(allData);
  }

}
</script>
4

1 に答える 1