Thursday, December 06, 2018

Google Sheets SDR Touch Exporter - Version 2.0

Simone was finding value in my Google Sheets SDR Touch Exporter plugin, but had a suggested improvement. Could I make it so that all sheets in the document are exported, not just the first?

I can, and I did. Check out version 2 of my code in this sample document. And below is the complete source code for the plugin.

I have to say, it was a real treat learning my little script was helping someone!

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Export Frequencies', functionName: 'sdrFreqExport'}
  ];
  spreadsheet.addMenu('SDR Tools', menuItems); 
}

function sdrFreqExportSheet(sheet) {
  // This represents ALL the data
  var name = sheet.getName();
  var range = sheet.getDataRange();
  var values = range.getValues();

  var doc    = ['<category id="1" name="' + name + '">'];
  for (var i = 2; i < values.length; i++) {
    doc.push('  <preset id="' + i + '" ' +
             '          name="' + values[i][2] + '" ' +
             '          freq="' + rawFreq(values[i][1]) + '" ' +
             '          centfreq="' + rawFreq(values[i][1]) + '" ' +
             '          offset="0" ' +
             '          order="' + i + '" ' +
             '          filter="13686" dem="0"/>');
  }
  doc.push(' </category>');
  return doc.join("\n");
}

function sdrFreqExport() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var doc =  ['<?xml version="1.0" encoding="UTF-8"?>',
              '<sdr_presets version="1">'];
  for(var i = 0; i < ss.getSheets().length; i++) {
    doc.push(sdrFreqExportSheet(ss.getSheets()[i]));
  }
  doc.push('</sdr_presets>');
  
  showDoc(doc.join("\n"));

}

function rawFreq(freq, band) {
  return freq * 1000000;
}

function showDoc(doc) {
  var src = "<pre><![CDATA[" + doc + "]]></pre>";  
  var html = HtmlService.createHtmlOutput(src)
      .setWidth(400)
      .setHeight(300);
  SpreadsheetApp.getUi()
      .showModalDialog(html, 'SDR Export');
}

No comments:

Post a Comment