Google App Script无法在首次提交表单时使用

时间:2016-03-14 07:19:28

标签: google-apps-script google-sheets google-form

提前感谢您的帮助。我有一个脚本创建和两个文件夹,并将两个不同的文件夹复制到新创建的文件夹中。然后它将文件夹移动到包含所有新创建的文件夹的特定文件夹中。我的脚本适用于手动运行,所有提交都不是第一次提交表单。这是我的剧本:

function onFormSubmit() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();//Returns the currently active spreadsheet, or null if there is none.
  var sheet = ss.getActiveSheet();//Gets the active sheet in a spreadsheet.
  var lastRow = sheet.getLastRow();//Returns the position of the last row that has content.
  var projectNameRange = sheet.getRange(lastRow, 2);//Returns the range with the top left cell at the given coordinates.
  var projectName = projectNameRange.getValues();//Returns the value of the top-left cell in the range. 
  var sh = SpreadsheetApp.getActiveSheet()
  var startcell = sh.getRange('D2').getValue();
  if(! startcell){sh.getRange('D2').setValue(300001);return};
  var colValues = sh.getRange('D2:D').getValues();// get all the values in column D in an array
  var max=0;// define the max variable to a minimal value
    for(var r in colValues){ // iterate the array
      var vv=colValues[r][0].toString().replace(/[^0-9]/g,'');// remove the letters from the string to convert to number
    if(Number(vv)>max){max=vv};// get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast
   }
   max++ ; // increment to be 1 above max value
sh.getRange(sh.getLastRow(), 4).setValue(Utilities.formatString('%06d',max));// and write it back to sheet's last row.
  var projectIdRange = sheet.getRange(lastRow, 4);
  var projectId = projectIdRange.getValues();//Returns the value of the top-left cell in the range. 
  var clientNameRange = sheet.getRange(lastRow, 3);
  var clientName = clientNameRange.getValues();
  var curDate = Utilities.formatDate(new Date(), "GMT+1", "yyyy")
  var targetProduction = DriveApp.createFolder(projectId + "_" + "Prod" + "_" + clientName + "_" + projectName + "_" + curDate);
  var targetCreative = DriveApp.createFolder(projectId + "_" + "Cre" + "_" + clientName + "_" + projectName + "_" + curDate);
  var sourceProductionFolder = "TestTempFolder";
  var sourceCreativeFolder = "TestTempFolder2";
  var sourceProduction = DriveApp.getFoldersByName(sourceProductionFolder);
  var sourceCreative = DriveApp.getFoldersByName(sourceCreativeFolder);


 if (sourceProduction.hasNext()) {
    copyFolderProduction(sourceProduction.next(), targetProduction);

  }

  if (sourceCreative.hasNext()) {
    copyFolderCreative(sourceCreative.next(), targetCreative);
  }

    DriveApp.getFolderById('0BwrizIzPM38bUGFPV2E1Q0JSMms').addFolder(targetProduction);
 DriveApp.removeFolder(targetProduction);
   DriveApp.getFolderById('0BwrizIzPM38bUGFPV2E1Q0JSMms').addFolder(targetCreative);
  DriveApp.removeFolder(targetCreative);


  function copyFolderProduction(sourceProduction, targetProduction) {

  var folders = sourceProduction.getFolders();
  var files  = sourceProduction.getFiles();

  while(files.hasNext()) {
    var file = files.next();
    file.makeCopy(file.getName(), targetProduction);
  }

  while(folders.hasNext()) {
    var subFolder = folders.next();
    var folderName = subFolder.getName();
    var targetFolder = targetProduction.createFolder(folderName);
    copyFolderCreative(subFolder, targetFolder);
  }
  }
  function copyFolderCreative(sourceCreative, targetCreative) {

   var folders = sourceCreative.getFolders();
   var files   = sourceCreative.getFiles();

  while(files.hasNext()) {
    var file = files.next();
    file.makeCopy(file.getName(), targetCreative);
  }

  while(folders.hasNext()) {
    var subFolder = folders.next();
    var folderName = subFolder.getName();
    var targetFolder = targetCreative.createFolder(folderName);
    copyFolderCreative(subFolder, targetFolder);
   }
  }
 }

我不确定我是在遗漏某些东西还是做错了什么。但是如果我手动运行脚本或者在第一个脚本之外的任何提交上,它确实有效。我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果从新创建的电子表格运行,则首先需要由人员打开并授予运行权限。 我自己遇到过这个问题,并通过创建一个小的“初始化权限”脚本来解决它。这可以在首次创建电子表格时运行,以便授予所有必要的权限,并且实际上什么都不做。但该代码强制Google Apps寻求使用所有必要应用的权限。 然后,您可以在表单提交时触发您的脚本,而无需让某人打开您的电子表格。

function initialisePermissions() {
  // MailApp
  MailApp.getRemainingDailyQuota();
  // Docs
  var throwawayDoc = DocumentApp.create('Initialising Doc').getId();
  DriveApp.getFileById(throwawayDoc).setTrashed(true);
  // Sheets
  var throwawaySs = SpreadsheetApp.create('throwawaySs').getId();
  DriveApp.getFileById(throwawaySs).setTrashed(true);
  // Forms
  var throwawayForm = FormApp.create('throwawayForm').getId();
  DriveApp.getFileById(throwawayForm).setTrashed(true);
  // Drive
  DriveApp.getStorageLimit();

  var sheet = SpreadsheetApp.getActive();
  ScriptApp.newTrigger("onFormSubmit").forSpreadsheet(sheet).onFormSubmit().create();

  SpreadsheetApp.getUi().alert("Thank you, this script can now run when a client submits their booking form."); 

  return;
};