是否可以在指定的文件夹中创建电子表格,或者我是否必须使用Drive API在之后移动它?
答案 0 :(得分:5)
我有点迟了,但我发现的方式不需要复制/删除文件。您只需删除并添加父项。我的代码是Ruby而不是JS,但我确信存在类似的东西。
file = service.get_file(file_id,fields: 'parents')
service.update_file(
file_id,
file
remove_parents: file.parents,
add_parents: new_folder_id,
fields: 'id,parents'
)
在搜索drive api docs几分钟后,我发现Node.JS下面的代码将文件移动到特定文件夹中。希望这会有所帮助。
fileId = '1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ'
folderId = '0BwwA4oUTeiV1TGRPeTVjaWRDY1E'
// Retrieve the existing parents to remove
drive.files.get({
fileId: fileId,
fields: 'parents'
}, function(err, file) {
if (err) {
// Handle error
console.log(err);
} else {
// Move the file to the new folder
var previousParents = file.parents.join(',');
drive.files.update({
fileId: fileId,
addParents: folderId,
removeParents: previousParents,
fields: 'id, parents'
}, function(err, file) {
if(err) {
// Handle error
} else {
// File moved.
}
});
}
});
答案 1 :(得分:2)
取决于您想如何使用以及如何创建电子表格,可以像这样创建电子表格。
function create_ss_in_folder(folder, name) {
var ss = SpreadsheetApp.create(name);
var id = ss.getId();
var file = DriveApp.getFileById(id);
var folder = get_folder_by_name(folder);
folder.addFile(file);
DriveApp.getRootFolder().removeFile(file);
return id;
}
再次没有太多信息我不知道为什么你会在有程序化的方式时手动创建电子表格。
一些有用的链接:
https://developers.google.com/apps-script/reference/drive/folder
https://developers.google.com/apps-script/advanced/drive
更新
//Move it to desired folder
var fileID = 'Your file ID'
var folderID = 'Folder ID'
var file = DriveApp.getFileById(fileID).getName()
var folder = DriveApp.getFolderById(folderID)
var newFile = file.makeCopy(file, folder)
// This will remove it from root.
DriveApp.getFileById(fileID).setTrashed(true)
答案 2 :(得分:2)
使用驱动器API创建一个空工作表,然后使用工作表api将其打开:
function getClient()
{
$client = new \Google_Client();
putenv(
'GOOGLE_APPLICATION_CREDENTIALS='.__DIR__."/config/google-creds.json"
);
$client = new \Google_Client();
$client->setScopes(
[
\Google_Service_Drive::DRIVE,
\Google_Service_Storage::CLOUD_PLATFORM,
'https://www.googleapis.com/auth/spreadsheets',
]
);
$client->useApplicationDefaultCredentials();
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
$ROOT_FOLDER_ID='you-must-set-this-to-your-folder-id';
// create the empty sheet:
$googleServiceDriveDriveFile = new \Google_Service_Drive_DriveFile();
$googleServiceDriveDriveFile->setMimeType(
'application/vnd.google-apps.spreadsheet')
;
$googleServiceDriveDriveFile->setName('Test');
$googleServiceDriveDriveFile->setParents([$ROOT_FOLDER_ID]);
$res = $service->files->create($googleServiceDriveDriveFile);
// list the files in the folder:
print 'Created file with id : ' . $res->getId() . "\n";
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name)',
'q' => "'$ROOT_FOLDER_ID' in parents"
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
}
}
// fetch the sheet you created and edit it.
$service = new Google_Service_Sheets($client);
$sheet = $service->spreadsheets->get($res->getId());
print "Fetched sheet with name: " . $sheet->getSpreadsheetUrl() . "\n";
答案 3 :(得分:1)
@ukaric给出的方法的略微修改版本。这将在与当前电子表格相同的文件夹中创建一个新表单(可以是您需要的任何文档类型)。这也可以用于移动文件,然后只需将创建部分替换为对目标文件的引用即可。
function createInCurrentFolder() {
// Create a new form in drive root
var form = FormApp.create("New_Form");
var formFile = DriveApp.getFileById(form.getId())
// Get the parent folders of the open document
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssFile = DriveApp.getFileById(ss.getId());
var ssParents = ssFile.getParents();
// Check if we have a parent and
// assume the first parent to be the current folder
if (ssParents.hasNext()){
var parentFolder = ssParents.next();
// Check for root folder
if (parentFolder.getId() == DriveApp.getRootFolder().getId()) return;
// Add the created form to current folder
// and remove it from root folder
parentFolder.addFile(formFile);
DriveApp.removeFile(formFile);
}
}