1.通过启用Advanced Drive Service
,您可以在 Apps 脚本中使用任何 Drive API 方法
语法是
Drive.Files.insert(resource, mediaData, optionalArgs)
2。您可以执行Url fetch 请求
其中的语法是
UrlFetchApp.fetch(url, options);
并且可以从method description推导出url和选项。
更新
如何使用 url fetch 在应用程序文件夹中创建文件的示例:
代码.gs
function uploadToSharedDrive(){
var url = "https://www.googleapis.com/upload/drive/v3/files?supportsTeamDrives=true&uploadType=resumable";
var blob = Utilities.newBlob('This is the content!');
var metaData = {
'name' :'config.json',
'parents' :["appDataFolder"]
}
params = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
},
contentType: 'application/json',
method: 'post',
payload: JSON.stringify(metaData),
}
var response = UrlFetchApp.fetch(url, params);
var data = blob.getBytes();
var params2 = {
method: "put",
payload: data,
muteHttpExceptions: true,
};
location = response.getHeaders().Location;
var response = UrlFetchApp.fetch(location, params2);
Logger.log(response.getContentText())
}
要使此代码正常工作,您需要edit the manifest 并提供必要的scopes。
manifest.js:
{
...
"oauthScopes": ["https://www.googleapis.com/auth/drive.appdata", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request"]
...
}
请记住,您创建的文件将无法在应用程序之外访问 - 这是预期行为。