Home > Net >  Google Apps Script createFile Not Working
Google Apps Script createFile Not Working

Time:05-06

Can anyone suggest why this code is not working. I simply want to create a Google Sheets file in a given folder. It keeps failing at - folder.createFile(evidenceLogName, '', MimeType.GOOGLE_SHEETS); - with the error - Exception: Invalid argument: file contentType.

    function createEvidenceLog() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName('Config');
      var evidenceFolder = sheet.getRange('EvidenceLibrary').getValue();
      var evidenceLogName = sheet.getRange('EvidenceLog').getValue();
      //var folder = DriveApp.getFoldersByName(evidenceFolder); 
      var folder = DriveApp.getFolderById("###");
      Logger.log(folder);
      folder.createFile(evidenceLogName, ' ', MimeType.GOOGLE_SHEETS);

    }

CodePudding user response:

Try this

function createEvidenceLog() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Config');
  var evidenceLogName = sheet.getRange('EvidenceLog').getValue();
  var folder = DriveApp.getFolderById("###########");
  var file = DriveApp.getFileById(SpreadsheetApp.create(evidenceLogName).getId())
  file.moveTo(folder)
}

create

moveTo

CodePudding user response:

I got it to work with this code:

function createEvidenceLog() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Config');
  var evidenceFolder = sheet.getRange("EvidenceLibrary").getValue();
  Logger.log(evidenceFolder);
  var evidenceFolderID = DriveApp.getFoldersByName(evidenceFolder).next().getId();
  Logger.log(evidenceFolderID);
  var evidenceLogName = sheet.getRange("EvidenceLog").getValue();
  Logger.log(evidenceLogName);
  var evidenceLogSheets = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: [evidenceLogName], parents: [{id: evidenceFolderID}]});

}
  • Related