Home > Net >  Exporting files from Google Drive GAS
Exporting files from Google Drive GAS

Time:10-14

I have a Google form. I plan to receive PDF documents and Images from users. All answers from the form are saved in the Google sheet.

Next, I need to send this document along with other text fields to my web service.

I found a method to export files from Google Drive by ID - https://developers.google.com/drive/api/v3/reference/files/export

However, due to corporate policy, I don't have enough rights to create OAuth 2.0 accesses - https://console.cloud.google.com/projectselector2/apis/credentials?supportedpurview=project

How else can I export files from Google drive?

function Main() {

  let arr = new Array()
  let table = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`Ответы на форму (1)`)
  let count = table.getLastRow()
  let record = table.getRange(count, 2, 1, 16).getValues()

  let email = record[0][0]
  let permission = record [0][1]
  let organization = record[0][2]
  let phone = record[0][3]
  phone = phone.substr(2)
  let target = record[0][6]
  let fullName = record[0][7].trim()
  let gender = record[0][8]
  let inn = record[0][9].toString()
  let snils = record[0][10].toString()
  let passport = record[0][11].toString()
  let issueDate = record[0][12]
  let issueOrganizationId = record[0][13].toString()
  let birthDate = record[0][14].toString()
  let file = record[0][15].replace('https://drive.google.com/open?id=', '').replace('https://drive.google.com/file/d/','').replace('/view?usp=sharing','').trim()

  arr.push(email, organization, phone, target, fullName, gender, inn, snils, passport, issueDate, issueOrganizationId,birthDate, file)
  Logger.log (arr)

  if (record[0][1] == 'Перевыпуск ЭП' && record[0][2] == 'Нет, не менялись') {
    try {
      //API_Renew(organization, email, phone)
    } catch (err) {
      SendEmail(err, email)
    }
  }
  else {
    try {
      //API_New(organization, email, fullName, phone, passport, inn, snils, target, gender, issueDate, issueOrganizationId, birthDate, file)
    } catch (err) {
      SendEmail(err, email)
    }
  }
}


function API_New (organization, email, fullName, phone, passport, inn, snils, target, gender, issueDate, issueOrganizationId, birthDate, file) {

  fullName = fullName.split(' ')

  let payload = [
    {
      "Organization": organization,
      "LastName": fullName[0],
      "FirstName": fullName[1],
      "MiddleName": fullName[2],
      "Email": email,
      "Phone": phone,
      "Series": passport.substring(0, 4),
      "Number": passport.substring(4),
      "Inn": inn,
      "Snils": snils,
      "Target": target, 
      "Gender": gender,
      "IssueDate": issueDate,
      "IssueOrganizationId": issueOrganizationId,
      "BirthDate": birthDate,
      "File": file
    }
  ]

  let options =                                                                                                         //Параметры отправки запроса (POST   ключ разработчика)
  {
    method: "POST",
    contentType: 'application/json; charset=utf-8',
    payload: JSON.stringify(payload)
  }

  
  return UrlFetchApp.fetch(URL, options)
}

CodePudding user response:

Thank you all for your comments!

I found 2 ways to export files from Google Drive.

Without connecting additional services:

function exportPDF_url (url) {
    let request = 
    {
        method: "GET", 
        headers: { "Authorization":"Bearer "   ScriptApp.getOAuthToken() }
    }
    return UrlFetchApp.fetch(url, request).getBlob();
}

With the connection of the Drive API service in the Apps Script project:

function exportPDF_id (url) {
  let fileID = url.replace('https://drive.google.com/open?id=', '')
  return  DriveApp.getFileById(fileID).getBlob();
}
  • Related