0

我有一个脚本,它在通过 UI 插入文件名后从 csv 文件导入数据。这一切都 100% 工作,文件被定位并且数据按预期导入。但是,该应用程序不会关闭,并且弹出式 UI 在屏幕上仍然处于活动状态。

我的脚本如下:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [
                     {name: "Import Employee Base Data", functionName: "importFromBaseCSV"},
                    ];
  ss.addMenu("User Functions", menuEntries);
}

//IMPORT BASE DATA FROM CSV

function importFromBaseCSV() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var importBaseDataApp = UiApp.createApplication().setTitle('Import BASE Data').setHeight(120).setWidth(350);

  var importBaseDataGrid = importBaseDataApp.createGrid(3, 2);
  importBaseDataGrid.setWidget(0, 0, importBaseDataApp.createLabel('Enter the File Date: '));
  importBaseDataGrid.setWidget(0, 1, importBaseDataApp.createTextBox().setName('baseDataFilename').setFocus(true).setWidth(150));
  importBaseDataGrid.setWidget(1, 0, importBaseDataApp.createLabel('e.g. 23092013'));
  importBaseDataGrid.setWidget(2, 0, importBaseDataApp.createLabel(''));

  var importBaseDataPanel = importBaseDataApp.createVerticalPanel();
  importBaseDataPanel.add(importBaseDataGrid);

  var importButton = importBaseDataApp.createButton('Import');
  var importHandler = importBaseDataApp.createServerHandler('importBaseData');
  importHandler.addCallbackElement(importBaseDataGrid);
  importButton.addClickHandler(importHandler);

  importBaseDataPanel.add(importButton);
  importBaseDataApp.add(importBaseDataPanel);
  ss.show(importBaseDataApp);

 }   

function importBaseData(e){

  var fileName = "C BS BASE " + e.parameter.baseDataFilename + ".csv";
  var files = DocsList.getFiles();
  var csvFile = "";

  for (var i = 0; i < files.length; i++) {
    if (files[i].getName() == fileName) {
      csvFile = files[i].getContentAsString();
      break;
    }
  }
  var csvData = CSVToArray(csvFile, ",");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Imported Base Data");
  for (var i = 0; i < csvData.length; i++) {
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
  }
}

// This will parse a delimited string into an array of
// arrays. The default delimiter is the comma, but this
// can be overriden in the second argument.

function CSVToArray( strData, strDelimiter ){
  // Check to see if the delimiter is defined. If not,
  // then default to comma.
  strDelimiter = (strDelimiter || ",");

  // Create a regular expression to parse the CSV values.
  var objPattern = new RegExp(
    (
      // Delimiters.
      "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

      // Quoted fields.
      "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

      // Standard fields.
      "([^\"\\" + strDelimiter + "\\r\\n]*))"
    ),
    "gi"
  );


  // Create an array to hold our data. Give the array
  // a default empty first row.
  var arrData = [[]];

  // Create an array to hold our individual pattern
  // matching groups.
  var arrMatches = null;


  // Keep looping over the regular expression matches
  // until we can no longer find a match.
  while (arrMatches = objPattern.exec( strData )){

    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[ 1 ];

    // Check to see if the given delimiter has a length
    // (is not the start of string) and if it matches
    // field delimiter. If id does not, then we know
    // that this delimiter is a row delimiter.
    if (
      strMatchedDelimiter.length &&
      (strMatchedDelimiter != strDelimiter)
    ){

      // Since we have reached a new row of data,
      // add an empty row to our data array.
      arrData.push( [] );

    }


    // Now that we have our delimiter out of the way,
    // let's check to see which kind of value we
    // captured (quoted or unquoted).
    if (arrMatches[ 2 ]){

      // We found a quoted value. When we capture
      // this value, unescape any double quotes.
      var strMatchedValue = arrMatches[ 2 ].replace(
        new RegExp( "\"\"", "g" ),
        "\""
      );

    } else {

      // We found a non-quoted value.
      var strMatchedValue = arrMatches[ 3 ];

    }


    // Now that we have our value string, let's add
    // it to the data array.
    arrData[ arrData.length - 1 ].push( strMatchedValue );
  }

  SpreadsheetApp.getActiveSpreadsheet().toast("...Completed", "Import Employee Base Data:", 3);

  // Return the parsed data.
  return( arrData );

  var importBaseDataApp = UiApp.getActiveApplication();
  importBaseDataApp.close();
  return importBaseDataApp;

}

任何人都可以帮忙吗?

4

1 回答 1

0

我只是快速看了一下,但试试这个。我将“Toast”和“Close”移到 clickHandler 调用的函数中。你想在他们的调用函数中控制 UI,它更流畅。有时您可能会很幸运,他们会关闭或更新在另一个被调用函数中调用它们。

function importBaseData(e){

  var fileName = "C BS BASE " + e.parameter.baseDataFilename + ".csv";
  var files = DocsList.getFiles();
  var csvFile = "";

  for (var i = 0; i < files.length; i++) {
    if (files[i].getName() == fileName) {
      csvFile = files[i].getContentAsString();
      break;
    }
  }
  var csvData = CSVToArray(csvFile, ",");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Imported Base Data");
  for (var i = 0; i < csvData.length; i++) {
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
  }
  SpreadsheetApp.getActiveSpreadsheet().toast("...Completed", "Import Employee Base Data:", 3);

  var importBaseDataApp = UiApp.getActiveApplication();
  importBaseDataApp.close();
  return importBaseDataApp;
}

// This will parse a delimited string into an array of
// arrays. The default delimiter is the comma, but this
// can be overriden in the second argument.

function CSVToArray( strData, strDelimiter ){
  // Check to see if the delimiter is defined. If not,
  // then default to comma.
  strDelimiter = (strDelimiter || ",");

  // Create a regular expression to parse the CSV values.
  var objPattern = new RegExp(
    (
      // Delimiters.
      "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

      // Quoted fields.
      "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

      // Standard fields.
      "([^\"\\" + strDelimiter + "\\r\\n]*))"
    ),
    "gi"
  );


  // Create an array to hold our data. Give the array
  // a default empty first row.
  var arrData = [[]];

  // Create an array to hold our individual pattern
  // matching groups.
  var arrMatches = null;


  // Keep looping over the regular expression matches
  // until we can no longer find a match.
  while (arrMatches = objPattern.exec( strData )){

    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[ 1 ];

    // Check to see if the given delimiter has a length
    // (is not the start of string) and if it matches
    // field delimiter. If id does not, then we know
    // that this delimiter is a row delimiter.
    if (
      strMatchedDelimiter.length &&
      (strMatchedDelimiter != strDelimiter)
    ){

      // Since we have reached a new row of data,
      // add an empty row to our data array.
      arrData.push( [] );

    }


    // Now that we have our delimiter out of the way,
    // let's check to see which kind of value we
    // captured (quoted or unquoted).
    if (arrMatches[ 2 ]){

      // We found a quoted value. When we capture
      // this value, unescape any double quotes.
      var strMatchedValue = arrMatches[ 2 ].replace(
        new RegExp( "\"\"", "g" ),
        "\""
      );

    } else {

      // We found a non-quoted value.
      var strMatchedValue = arrMatches[ 3 ];

    }


    // Now that we have our value string, let's add
    // it to the data array.
    arrData[ arrData.length - 1 ].push( strMatchedValue );
  }
  // Return the parsed data.
  return( arrData );

}
于 2013-09-23T13:02:37.247 回答