如何创建通过Google Apps脚本和放新的脚本文件;驱动器SDK [英] How to create a new script file via Apps Script & Drive SDK

查看:258
本文介绍了如何创建通过Google Apps脚本和放新的脚本文件;驱动器SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建文件的新项目与Google Apps脚本内驱动器的SDK调用。

Trying to create a new project with files with a call to the Drive SDK within Apps Script.

到底在哪会在 UrlFetchApp 要求下面去...

Where exactly would the following go in a UrlFetchApp request...

{
  "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"
    }
  ]
}

这是从导入/导出文档并在的例如视频丹提到的调用是语言的Google Apps脚本之外,但请求的脚本文件类型列表和内容这些文件<一个href=\"https://script.google.com/d/1rxooF5Ys7JuTUHeOWeQ-uifJPpp31VS2JcZ9YiHenC6zGd7r5hqXs2K2/edit?usp=sharing\"相对=nofollow>确实工作一旦授权设置与Eric的的oauth2库。

It's from the import/export docs and in the example video Dan mentions the calls are for languages outside of Apps Script but requesting a list of script file types and the content of those files does work once the authorization is setup with Eric's oAuth2 library.

我最近的猜测...

function createProject( ) {
  var token = getDriveService().getAccessToken(); // from Eric's 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 untitled Drive file of unknown type does get created and the payload does get inserted into it, but it's not converted to a script file type.

去另一条路线,只是使用...

Going 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的例子,但不知道有多少应该被翻译过(如果可能)服务器端的Google Apps脚本。

Also aware of the Drive insert docs that have a client-side JS example, but don't know how much of it should be translated over (if possible) to server-side Apps Script.

推荐答案

您是几乎没有,但你有几个错误。你的第一种方法(使用网​​址抓取)已在选项变量一些错误。下面是(验证授权后)创建一个新的Google Apps脚本项目并写入网址抓取响应页面的doGet()函数:

You're almost there, but you have a few mistakes. Your first approach (using UrlFetch) has some errors in the options variable. Here's a doGet() function that (after verifying authorization) creates a new Apps Script Project and writes the UrlFetch response to the page:

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());
  }
}

这code会在根云端硬盘文件夹名为无题Google Apps脚本项目文件。在选项对象应指定POST作为创建项目(默认是GET)的方法,和有效载荷需要转换为字符串从JSON对象由<理解code>网​​址抓取。

This code creates an Apps Script project file called "Untitled" in the root Drive folder. The options object should specify POST as the method for creating projects (default is GET), and the payload needs to be converted to a string from a JSON object to be understood by UrlFetch.

另外,您也可以通过在Google Apps脚本使用先进的驱动服务,而不是网​​址抓取的做同样的事情。下面code创建相同的Google Apps脚本的项目(但其放置在指定的文件夹并命名文件测试脚本')

Alternatively, you can do the same thing by using the Advanced Drive Service in Apps Script instead of UrlFetch. The following code creates the same Apps Script project (but places it in the specified folder and names the file '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"});
}

这篇关于如何创建通过Google Apps脚本和放新的脚本文件;驱动器SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆