2014-11-24 74 views
3

尝试通过在Apps脚本中调用Drive SDK的文件创建新项目。如何通过Apps脚本和驱动器SDK创建新的脚本文件

到底在哪会在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" 
    } 
    ] 
} 

这是从import/export docs,并在example video丹提到呼叫针对的是语言应用脚本之外,但请求的脚本文件列表类型和这些文件的内容does work一旦授权与Eric的oAuth2库设置。

我最近的猜测......

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

} 

未知类型的驱动器无文件时获得创建和有效载荷并插入到了它,但它不是转换为一个脚本文件类型。

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

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

Drive.Files.insert(file); 

...抛出一个内部错误。

也知道Drive insert docs有一个客户端JS示例,但不知道它应该翻译多少(如果可能)到服务器端Apps脚本。

+0

在你选择的变量,尝试改变有效载荷参数的身体:“和忘记元数据。 – Rivero 2014-11-25 17:39:13

+0

未知类型的新Drive文件仍被创建,但未插入任何文本或任何内容。我在[UrlFetchApp文档](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app)中看不到'body'作为高级参数,并将'payload'解释为相当于。 – 2014-11-25 18:44:36

回答

4

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

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

此代码创建的根驱动器名为“无题” Google Apps脚本项目文件夹。 options对象应该指定POST作为创建项目的方法(默认为GET),并且负载需要从一个JSON对象转换为一个字符串以便被​​理解。

或者,您也可以使用Apps Script中的Advanced Drive Service而不是​​来做同样的事情。下面的代码创建相同的Apps脚本项目(但其放置在指定的文件夹并命名文件“测试脚本”):

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"}); 
} 
+0

'Utilities.newBlob()'是我错过的东西。由于某种原因,认为blob必须来自'response.getBlob()'。将在我的插件中实现。 thx一吨! – 2014-11-26 04:24:04

+0

等待,您是否手动为unicode'\ u003c'插入转换或者您是否使用了库/映射? – 2014-12-11 23:53:36

+0

我为此示例手动输入了unicode。 – 2014-12-12 01:01:12