Загрузить CSV-файл из App Maker в Big Query - PullRequest
0 голосов
/ 18 марта 2020

У меня есть приложение в App Maker (да, я знаю, что оно будет объявлено устаревшим в 2021 году), которое я использую для загрузки записей в Big Query из файла CSV или формы.

enter image description here

работает правильно в режиме предварительного просмотра, но не работает в развернутой версии.

enter image description here

Тем не менее В этом приложении у меня есть форма для отправки записей одна за другой в BigQuery, и она корректно работает в предварительном просмотре и развернутых версиях, поэтому я думаю, что мы можем исключить проблему с разрешениями ...

Фрагмент кода

function processCSV (file_id){

  var file = DriveApp.getFileById(file_id);
  var raw_data = file.getBlob().getDataAsString();
  var lines = raw_data.split("\n");

  var result_2darray = [];

  for (var i =1; i < lines.length; i++){

    var row = lines[i].split(",");
    var list_data = [];    

    var id_ = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
    var contacto = row[0] != null ? row[0] : null;
    //var id = row[1];
    var telefono = row[2];
    var empresa = row[3];

    list_data.push(id_,contacto,telefono,empresa);

    try {
      var result_list = addBigqueryFromCSV(list_data);
      //console.log(result_list.length);
      result_2darray.push(result_list); 
    } catch (e){
      console.error(e);
    }
  }


  //console.log(result_2darray.length);
  console.log(result_2darray);

  var id = "xxx";
  var folder_id = "xxx";
  var sheet_name = "xxx";

  var ss = SpreadsheetApp.openById(id).getSheetByName(sheet_name).getRange(1, 1, result_2darray.length, result_2darray[0].length).setValues(result_2darray);
  var ss_file = DriveApp.getFileById(id);

  var folder = DriveApp.getFolderById(folder_id);
  var fileName = ss_file.getName() + ".csv";
  var csvFile = convertRangeToCsvFile_(id, sheet_name);

  // create a file in the Docs List with the given name and the csv data
  var csv_final = folder.createFile(fileName, csvFile);

  var data = csv_final.getBlob().setContentType('application/octet-stream');


  var projectId = "gestion-leads-pyme";
  var job = {
    configuration: {
      load: {
        destinationTable: {
          projectId: "gestion-leads-pyme",
          datasetId: "gestion_leads",
          tableId: "lead"
        },

        schema: {
          fields: [
            {name: 'Contacto', type: 'STRING'},
            {name: 'ID', type: 'STRING'},
            {name: 'Telefono', type: 'NUMERIC'},
            {name: 'Empresa', type: 'STRING'}            
          ]
        },
        //skipLeadingRows: 1
        source_format: 'CSV',
      }
    }
  };

  job = BigQuery.Jobs.insert(job, projectId, data);
  Logger.log('Load job started. Check on the status of it here: ' + 'https://bigquery.cloud.google.com/jobs/%s', projectId);

  Logger.log(job.status);

  SpreadsheetApp.openById(id).getSheetByName(sheet_name).clear();
  DriveApp.getFileById(csv_final.getId()).setTrashed(true);

}

Полное сообщение об ошибке

Извините, консоль находится в испанском sh, и у меня нет прав для ее изменения.

enter image description here

Строка, которая, по-видимому, вызывает ошибку:

var file = DriveApp.getFileById(file_id);
...