简体   繁体   English

Google Apps脚本中的createFile()无法正常运行

[英]createFile() in google Apps Script is not functioning properly

I am trying to create a file. 我正在尝试创建一个文件。 It works fine when I run the following code segment from the debugger in apps script. 当我从应用程序脚本中的调试器运行以下代码段时,它工作正常。 However, when I run it real time from the spreadsheet, it says I do not have permission to call createfile. 但是,当我从电子表格实时运行它时,它说我没有调用createfile的权限。 Everything that is logged is identical. 记录的所有内容都是相同的。 The issue is not I do not have authority as I am the only one in the spreadsheet and am the owner. 问题不在于我没有权限,因为我是电子表格中的唯一人员并且是所有者。 The purpose of the CSV is to move it from my google drive into data for BigQuery CSV的目的是将其从我的Google驱动器移至BigQuery的数据中

function saveAsCSV(row) { //Doc to Csv
//row = 3; //when this is uncommented and ran from the debugger, it works.

 try{

var fileName=  Date.now()
fileName = fileName + ".csv";


var csvFile = convertRangeToCsvFile_(fileName,row);
Logger.log(csvFile); //Both times ran on the spreadsheet and from debug equals the same.

DriveApp.createFile(fileName, csvFile);


SpreadsheetApp.getActiveSpreadsheet().getSheetByName("New and Open").getRange("J" + row.toString()).setValue("");

loadCsv(fileName);

}
catch(e){Logger.log("B" + e.message);} //No permission to create file
}



function convertRangeToCsvFile_(csvFileName, r) {

var ws = SpreadsheetApp.getActiveSpreadsheet();
try {
//var data = ws.getValues();
var csvFile = undefined;

var csv = "";
var row = r;

var datArray = Create2DArray(1,19);


  datArray[0][0] =  ws.getRange("A" + row.toString()).getValue().toString().toUpperCase();
  datArray[0][1] =  ws.getRange("B"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][2] =  ws.getRange("C"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][3] =  ws.getRange("D"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][4] =  ws.getRange("E"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][5] =  ws.getRange("F"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][6] =  ws.getRange("G"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][7] =  ws.getRange("H"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][8] =  ws.getRange("I"+row.toString()).getValue().toString().toUpperCase();
  datArray[0][9] =  new Date(ws.getRange("K"+row.toString()).getValue().toString()).getHours();
  datArray[0][10] =  new Date(ws.getRange("K"+row.toString()).getValue().toString()).getMinutes();
  datArray[0][11] =  new Date(ws.getRange("L"+row.toString()).getValue().toString()).getHours();
  datArray[0][12] =  new Date(ws.getRange("L"+row.toString()).getValue().toString()).getMinutes();
  datArray[0][13] =  new Date(ws.getRange("M"+row.toString()).getValue().toString()).getHours();
  datArray[0][14] =  new Date(ws.getRange("M"+row.toString()).getValue().toString()).getMinutes();
  datArray[0][15] =  new Date(ws.getRange("N"+row.toString()).getValue().toString()).getTime();
  datArray[0][16] =  new Date(ws.getRange("N"+row.toString()).getValue().toString()).getFullYear();
  datArray[0][17] =  new Date(ws.getRange("N"+row.toString()).getValue().toString()).getMonth();
  datArray[0][18] =  new Date(ws.getRange("N"+row.toString()).getValue().toString()).getDate();

for(var i = 0; i < 19; i++){
  if(datArray[0][i] == ""){if(i > 9){datArray[0][i] = 0;} else{datArray[0][i] = "nil";} } 
  if(i < 18){csv += '"' + datArray[0][i] + '"' + ",";}
  else{ csv += '"' + datArray[0][i] + '"'; }
}

Logger.log("A " + csv);
Logger.log(csv + "\n" + datArray[0].join(","));
csvFile = csv;
return csvFile;
}
  catch(err) {
    Logger.log("C" + err);
    Browser.msgBox(err);
   }
}

You mention in your comment on my answer that you are using onEdit to trigger the script. 您在对我的答案的评论中提到您正在使用onEdit触发脚本。 Since this is a Simple Trigger, your current approach will not work. 由于这是一个简单触发器,因此您当前的方法将行不通。 When you use simple triggers to run an Apps Script, it runs in a sandbox with reduced permissions. 当您使用简单的触发器运行Apps脚本时,它将在权限降低的沙箱中运行。

See: https://developers.google.com/apps-script/guides/triggers/#restrictions 请参阅: https : //developers.google.com/apps-script/guides/triggers/#restrictions

The best I can recommend is create a custom menu option with a UI popup asking for the row number to export. 我推荐的最好方法是创建一个带有UI弹出窗口的自定义菜单选项,要求导出行号。 If the code is triggered from a menu by the user, it runs with full permission to access that users account. 如果该代码是由用户从菜单触发的,则它将以访问该用户帐户的完全权限运行。

Depending on your use-case, a scheduled trigger might work too. 根据您的用例,计划的触发器也可能起作用。 It could run every 10 minutes or every Hour and export any changes to the spreadsheet. 它可以每10分钟或每小时运行一次,并将所有更改导出到电子表格。 In this case the Apps Script runs as you, with permission to access your account, and the resulting CSV would be created on your drive. 在这种情况下,Apps脚本将以您的身份运行,并且有权访问您的帐户,并且将在驱动器上创建生成的CSV。

Details on how to create a custom menu: https://developers.google.com/apps-script/guides/triggers/#onopen 有关如何创建自定义菜单的详细信息: https : //developers.google.com/apps-script/guides/triggers/#onopen

Details on how to create a form for the user: https://developers.google.com/apps-script/guides/ui-service 有关如何为用户创建表单的详细信息: https : //developers.google.com/apps-script/guides/ui-service

Details on time driven triggers: https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers 有关时间驱动触发器的详细信息: https : //developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM