Home > Mobile >  Problem trying to save my google sheet as a pdf to my drive folder
Problem trying to save my google sheet as a pdf to my drive folder

Time:06-04

  1. The script works properly as it should when i use it from my gmail acc
  2. but when I share it to another gmail ACC the script keeps getting errors!
  3. The error Exception: Access denied: DriveApp.

I use this code to save a specific sheet to my drive as a pdf and mail it to a specific person. I want the code to run normally when other users are granted permission to use this sheet. Please help me solve this problem.

function sendReport(range) {
  SpreadsheetApp.getActive().getSheetByName("Customers").hideSheet();
    SpreadsheetApp.getActive().getSheetByName("Products").hideSheet();
      SpreadsheetApp.getActive().getSheetByName("SO Log").hideSheet();
        SpreadsheetApp.getActive().getSheetByName("SOF").hideSheet();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var SOSheet = ss.getSheetByName("Sales Order");
  var name = SOSheet.getRange('F10').getValue();
  

  var message = {
    to: "[email protected]",
    subject: "Sales Order",
    body: "Hi team,\n\nPlease find the Sales Order attached.\n\nThank you",
    name: "Dave",
    attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("SO" name)]
  }
  MailApp.sendEmail(message);

  SpreadsheetApp.getActive().getSheetByName("Customers").showSheet();
  SpreadsheetApp.getActive().getSheetByName("Products").showSheet();
  SpreadsheetApp.getActive().getSheetByName("SO Log").showSheet();
  SpreadsheetApp.getActive().getSheetByName("SOF").showSheet();
  

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var token = ScriptApp.getOAuthToken();
  var sheet = ss.getSheetByName("Sales Order");
  var bogus = DriveApp.getRootFolder();


  //Creating an exportable URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/exSOrt?".replace("SS_ID", ss.getId());
  var folderID = "1KU7ylGsci9qsVzxU2ZlCW"; // Folder id to save in a folder.
  var folder = DriveApp.getFolderById(folderID);
  var SOcode = ss.getRange("'Sales Order'!F10").getValue()
  var pdfName = "SO"   SOcode;
  

  /* Specify PDF exSOrt parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
  */
  var url_ext = 'exportFormat=pdf&format=pdf'        // exSOrt as pdf / csv / xls / xlsx
    '&size=A4'                       // paper size legal / letter / A4
    '&SOrtrait=true'                    // orientation, false for landscape
    '&fitw=true&source=labnol'           // fit to page width, false for actual size
    '&sheetnames=false&printtitle=false' // hide optional headers and footers
    '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
    '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
    '&gid=';                             // the sheet's Id
    
  // Convert individual worksheet to PDF

  var response = UrlFetchApp.fetch(url   url_ext   sheet.getSheetId(), {
    headers: {
      'Authorization': 'Bearer '   ScriptApp.getOAuthToken()},muteHttpExceptions:true});
    
    
  
  ;
  

var blobs = response.getBlob().setName(pdfName   '.pdf');


var folders = folder.getFoldersByName(pdfName);
folder = folders.hasNext() ? folders.next() : folder.createFolder(pdfName);


var newFile = folder.createFile(blobs);
var newFileLink = newFile.getUrl();
var SOcode = ss.getRange("'SOF'!F12").getValue();
var writePDFLink =  ss.getRange("'SOF'!L1").setValue(newFileLink);

  
  // Define the scope
  Logger.log("Storage Space used: "   DriveApp.getStorageUsed());

}

CodePudding user response:

The method enter image description here

  • Related