Google Script to import CSV file from Google Drive to G Sheets when specific cell is edited

让人想犯罪 __ 提交于 2021-02-11 13:06:09

问题


I have a script in my G Sheets that imports a CSV from Google Drive and I have attached it to a clickable button within the workbook. It works great. Here is the script:

function myFunction() {

  var file = DriveApp.getFilesByName("FileName.csv").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Imported File");
  sheet.getRange('A1:K10000').clearContent();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

However, I would like to automate the process further by having it run this function when a specific cell is edited. I will run a webhook to automate changing this cell.

So, as a trial run I found a very helpful stackoverflow question here and created this code to simply clear the cells:

function onEdit(e) {     
  if (e.range.getSheet().getName() === 'Imported File') {
    if (e.range.getA1Notation() === 'L2') {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Imported File");
  sheet.getRange('A1:K10000').clearContent();

    }
  }
}

Again, this works great. So my issue is that whenever I put the full original code inside the edit function it does not work. Have I missed something?

function onEdit(e) {     
  if (e.range.getSheet().getName() === 'Imported File') {
    if (e.range.getA1Notation() === 'L2') {

  var file = DriveApp.getFilesByName("FileName.csv").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Imported File");
  sheet.getRange('A1:K10000').clearContent();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

    }
  }
}

回答1:


Simple triggers such as onEdit() have ScriptApp.AuthMode.LIMITED permissions, so they do not have access to services such as Drive.

From the documentation:

LIMITED A mode that allows access to a limited subset of services. This mode occurs when an add-on or a script bound to a document executes an onOpen(e) or onEdit(e) simple trigger, except in the case described for NONE.

Solution:

You can create an installable trigger on the fly, e.g. when the webhook is run like this:

function createTrigger() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ScriptApp.newTrigger('csvEdit').forSpreadsheet(ss).onEdit().create();
}

Run the function manually first to establish the necessary DriveApp permissions. Then the new trigger will work as usual, when L2 cell is edited.

function csvEdit(e) {     
  if (e.range.getSheet().getName() === 'Imported File') {
    if (e.range.getA1Notation() === 'L2') {

  var file = DriveApp.getFilesByName("FileName.csv").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Imported File");
  sheet.getRange('A1:K10000').clearContent();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

    }
  }
}


来源:https://stackoverflow.com/questions/65326421/google-script-to-import-csv-file-from-google-drive-to-g-sheets-when-specific-cel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!