【问题标题】:Write text to a google doc file with google-api-ruby-client使用 google-api-ruby-client 将文本写入 google doc 文件
【发布时间】:2020-10-06 15:11:55
【问题描述】:

我正在尝试使用 google-api-ruby-client 将一些文本写入新的 google doc 文件。

我认为以下应该有效,但它没有:

file_metadata = {
  name: "Testing",
  mime_type: 'application/vnd.google-apps.document'
}

@drive.create_file(file_metadata,
                   fields: 'id',
                   upload_source: StringIO.new("tt"),
                   content_type: 'text/text')

当我运行它时,我得到Google::Apis::ClientError: badRequest: Bad Request


更新:在 Tanaike 在下面发布正确答案后,我还必须移动文件,因为它是在我的服务用户的根目录中创建的。最终(工作)代码如下所示

file_metadata = {
  name: "Filename",
  mime_type: 'application/vnd.google-apps.document'
}

file = @drive.create_file(file_metadata,
                   fields: 'id,parents',
                   upload_source: StringIO.new("Text to go in file"),
                   content_type: 'text/plain')

current_parent = file.parents.first
@drive.update_file(file.id, nil,
                   add_parents: "#{@folder_id}",
                   remove_parents: "#{current_parent}")

【问题讨论】:

    标签: ruby google-docs google-docs-api


    【解决方案1】:

    这个修改怎么样?在这种情况下,文本的 mimeType 是text/plain

    发件人:

    content_type: 'text/text'
    

    收件人:

    content_type: 'text/plain'
    

    注意:

    • 在我的环境中,当我使用content_type: 'text/text' 时,会出现同样的错误。当我使用content_type: 'text/plain' 时,包含tt 文本的Google 文档会创建到Google Drive 的根文件夹中。
    • 在这种情况下,它假设您的@drive 可以用于上传文件。

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2015-11-17
      • 1970-01-01
      相关资源
      最近更新 更多