【问题标题】:How to copy google doc to another folder in google drive service如何将谷歌文档复制到谷歌驱动服务中的另一个文件夹
【发布时间】:2020-02-28 16:12:17
【问题描述】:

我有一个类可以复制 google doc 模板文件,然后将占位符与数据合并。问题是它在与原始模板文件相同的文件夹中创建副本,我找不到将其复制到另一个文件夹的方法。

假设我在谷歌驱动器中有这样的结构。

Main
    TemplateFiles
        MainTemplateDoc
    Contracts
        CopyOfMainTemplateDocAfterBeingMerged

如您所见,CopyOfMainTemplateDocAfterBeingMerged 文档不会位于原始模板所在的 TemplateFiles 文件夹中,而是复制到 Contracts 文件夹中。

是否可以在创建副本后使用 google drive v2 服务来移动文件?我正在使用 .net Google.Apis.Drive.v2 nuget 包。这是我目前创建副本的代码。

private string CopyDocument(string documentId, string title)
{
    var newFile = new File { Title = title };
    var documentCopyFile = driveService.Files.Copy(newFile, documentId).Execute();
    return documentCopyFile.Id;
}

【问题讨论】:

    标签: google-api google-drive-api


    【解决方案1】:
    • 您正在使用 Drive API v2。
    • 您想将文件复制到Contracts 的文件夹中。
    • 您已经能够使用 Drive API。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    在这个答案中,我想提出以下修改。

    发件人:

    var newFile = new File { Title = title };
    

    收件人:

    var newFile = new File {
        Title = title,
        Parents = new List<ParentReference> {new ParentReference {Id = parentId}}
    };
    

    var newFile = new File() {
        Title = title
    };
    newFile.Parents = new List<ParentReference>() {new ParentReference() {Id = parentId}};
    

    var newFile = new File();
    newFile.Title = title;
    newFile.Parents = new List<ParentReference>() {new ParentReference() {Id = parentId}};
    
    • parentId 是文件夹 Contracts 的文件夹 ID。

    注意:

    • 如果您想使用 Drive API v3,请使用 new List&lt;string&gt; {folderId} 而不是 new List&lt;ParentReference&gt; {new ParentReference {Id = folderId}}

    参考资料:

    如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

    【讨论】:

    • 谢谢。我确实使用 updaterequest 找到了父母参考,但无法使其正常工作。它在这里工作得很好,所以我不太确定问题是什么,但是嘿,它正在工作。再次感谢!
    • @geoff swartz 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多