How to create a new script file through Apps script & Drive SDK

Trying to create a new project with files with an SDK on disk in Script applications.

Where exactly the request will follow UrlFetchApp...

{
  "files": [
    {
      "id":"9basdfbd-749a-4as9b-b9d1-d64basdf803",
      "name":"Code",
      "type":"server_js",
      "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
    },
    {
      "id":"3asf7c0d-1afb-4a9-8431-5asdfc79e7ae",
      "name":"index",
      "type":"html",
      "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    New message!!\n  \u003c/body\u003e\n\u003c/html\u003e"
    }
  ]
}

This is from import / export documents and in the video example, Dan mentions that calls are for languages ​​outside of Script applications, but requests a list of Script file types and the contents of these files work after authorization using the Eric oAuth2 library.

My last guess ...

function createProject( ) {
  var token = getDriveService().getAccessToken(); // from Eric oAuth2 lib

  var url = 'https://www.googleapis.com/upload/drive/v2/files?convert=true';

  // Where does this go?
  var files = {
    "files": [
      {
        "name":"Code",
        "type":"server_js",
        "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
      },
      {
        "name":"index",
        "type":"html",
        "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Hello, world!!\n  \u003c/body\u003e\n\u003c/html\u003e"
      }
    ]
  };

  // Where does this go too?
  var metadata = {
    'title': 'script-test1',
    'mimeType': 'application/vnd.google-apps.script',
    "parents": [
      {
        "id": "0B2VkNbQMTnaCODBRVjZQcXBXckU"
      }
    ],
  };

  var options = {
    'headers' : {
      'Authorization': 'Bearer ' +  token,
      'Content-Type': 'application/vnd.google-apps.script+json',
    },
    'method' : 'POST',
    'payload' : files  // probably not right

  };

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getResponseCode());

}

An unidentified Drive file of an unknown type is created, and the payload gets into it, but is not converted to the Script file type.

Switching to another route and just using ...

var file = {
    "title": "Test script",
    "mimeType": "application/vnd.google-apps.script",
    "parents": [
      {
        "id": "[INSERT FOLDER ID HERE]"
      }
    ]
  };

Drive.Files.insert(file);

... throws an internal error.

, JS , , ( ) Script.

+3
1

, . ( UrlFetch) options. doGet(), ( ) Apps Script UrlFetch :

function doGet() {
  var driveService = getDriveService();
  if (!driveService.hasAccess()) {
    var authorizationUrl = driveService.getAuthorizationUrl();
    var template = HtmlService.createTemplate(
        '<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
        'Refresh the page when authorization complete.');
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    return page;
  } else {     
    var url = "https://www.googleapis.com/upload/drive/v2/files?convert=true";   
    var requestBody =  {
      "files": [
        {
          "name":"Code",
          "type":"server_js",
          "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
        },
        {
          "name":"index",
          "type":"html",
         "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Created with Apps Script.\n  \u003c/body\u003e\n\u003c/html\u003e"
        }
      ]
    };

    var options = {
      "headers": {
         'Authorization': 'Bearer ' +  driveService.getAccessToken(),
       }, 
      "contentType": "application/vnd.google-apps.script+json",
      "method" : "post",
      "payload": JSON.stringify(requestBody)
    }

    var response = UrlFetchApp.fetch(url, options);
    return HtmlService.createHtmlOutput(response.getContentText());
  }
}

Script " " . options POST ( GET), JSON, UrlFetch.

, Script UrlFetch. Script ( "Test script" ):

function createGoogleFileInFolder3() {  
    var requestBody =  {
      "files": [
        {
          "name":"Code",
          "type":"server_js",
          "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
        },
        {
          "name":"index",
          "type":"html",
          "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Created with Apps Script.\n  \u003c/body\u003e\n\u003c/html\u003e"
        }
      ]
    };

  var resource = {
    "title": "Test script",
    "parents": [
      {
        "id": "<parent folder id here>"
      }
    ]
  };

  var blob = Utilities.newBlob(JSON.stringify(requestBody), "application/vnd.google-apps.script+json");

  Drive.Files.insert(resource, blob, {"convert":"true"});
}
+4

Source: https://habr.com/ru/post/1621809/


All Articles