Update value in a spreadsheet sidebar when next row is selected in table

后端 未结 2 1200
傲寒
傲寒 2021-01-27 02:07

To facilitate the annotation of audio files in a Google spreadsheet, I\'d like to implement an audio player in the sidebar which automatically plays the audio file mentioned as

2条回答
  •  余生分开走
    2021-01-27 02:56

    I made some small changes to the example code you provided so that the sidebar does not update periodically following the time interval.

    Basically, I've used PropertiesService to store the row that is selected. The idea is that the script checks whether the currently selected row and the previously selected row (the one selected last time getRecord was called, that is, during last interval) are the same. If they are the same, there hasn't been a row selection change, which means the audio in the sidebar doesn't need updating.

    So it only updates if the selected row changes, which is, I think, the main issue you are having.

    To achieve this, your code would have to be modified in the following way (look at inline comments for details on the changes):

    getRecord()

    function getRecord() {
      var scriptProperties = PropertiesService.getScriptProperties();
      var sheet = SpreadsheetApp.getActiveSheet();
      var data = sheet.getDataRange().getValues();
      var headers = data[0];
      var rowNum = sheet.getActiveCell().getRow(); // Get currently selected row
      var oldRowNum = scriptProperties.getProperty("selectedRow"); // Get previously selected row
      if(rowNum == oldRowNum) { // Check if the was a row selection change
        // Function returns the string "unchanged"
        return "unchanged";
      }
      scriptProperties.setProperty("selectedRow", rowNum); // Update row index
      if (rowNum > data.length) return [];
      var record = [];
      for (var col=0;col

    Depending on whether there was a selection change, getRecord returns:

    • a record array, if the selected row is different.
    • the string "unchanged", if the selected row is the same. Probably this is not the most elegant way to handle this, but you get the idea.

    Then, showRecord(record) gets this returned value. If this value is the string "unchanged", it won't update the sidebar:

    showRecord(record)

      function showRecord(record) {
        // Checks whether returned value is `"unchanged"` (this means the row selected is the same one as before)
        if (record != "unchanged" && record.length) {
          for (var i = 2; i <= 2; i++) {
            // build field name on the fly, formatted field-1234
            var str = '' + i;
            var fieldId = 'field-' + ('0000' + str).substring(str.length)
    
            // If this field # doesn't already exist on the page, create it
            if (!$('#'+fieldId).length) {
              var newField = $($.parseHTML('
    ')); $('#sidebar-record-block').append(newField); } // Replace content of the field div with new record $('#'+fieldId).replaceWith('
    '); $('#'+fieldId).append($('
    ' + record[i].heading + '
    ')) .append(''); } } // TODO: hide any existing fields that are beyond the current record length //Setup the next poll poll(); }

    I also added the autoplay attribute in this line:

    .append('')
    

    So that the audio plays automatically when you select a new row, without having to click the play button.

    Finally, I changed the poll interval to 500, so that you don't have to wait so much for the new audio to play. Anyway you can edit this to whatever suits you best:

    interval = interval || 500;
    

    I didn't modify the rest of the script, even though it can probably be improved owing to the fact that it was mainly written for a different issue.

    I hope this is of any help.

提交回复
热议问题