尝试通过在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脚本。
在你选择的变量,尝试改变有效载荷参数的身体:“和忘记元数据。 – Rivero 2014-11-25 17:39:13
未知类型的新Drive文件仍被创建,但未插入任何文本或任何内容。我在[UrlFetchApp文档](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app)中看不到'body'作为高级参数,并将'payload'解释为相当于。 – 2014-11-25 18:44:36