使用R中的googlesheets覆盖工作表

问题描述 投票:5回答:2

我有两个问题。

  1. 如何使用R中的googlesheets包覆盖现有电子表格中的工作表?
  2. 如何使用R中的googlesheets包在现有电子表格中创建新工作表?

我在the documentation.找不到任何东西

r overwrite google-sheets-api worksheet r-googlesheets
2个回答
3
投票

您可以使用gs_edit_cells()选项直接使用trim = TRUE覆盖工作表中的数据,以清除工作表中的任何额外内容。正如文档所指出的那样,使用这个函数,因此,任何依赖它的函数(如果gs_ws_new()不是NULL,包括input)将是extremely slow

唯一可用的其他选项是构建一个包含所有感兴趣的工作表(例如.xlsx)的完整文件,并使用gs_upload(),它将覆盖整个文件。


1
投票

要在现有电子表格中添加新工作表,请执行以下操作:

require(googlesheets)
#first get the existing spreadsheet
existing_spreadsheet <- gs_title("title")  
#Then add the new worksheet to the existing sheet 
gs_ws_new(existing_spreadsheet
       , ws_title = "worksheet title"  #make sure it doesn't exist already
       , input = your_input #data.frame or data.table
       , trim = TRUE  #optional if you want your worksheet trimed
              )

我自己无法找到直接覆盖现有电子表格中工作表的方法。所以我不得不删除现有的工作表并将其再次添加为新的工作表。

#first delete the existing worksheet
existing_spreadsheet <- gs_ws_delete(existing_spreadsheet, "work sheet title you want updated")
# Then add the newworksheet with new data
gs_ws_new(existing_spreadsheet
        , ws_title = "worksheet title" 
        , input = your_new_data #data.frame or data.table
        , trim = TRUE  #optional if you want your worksheet trimed
      )
© www.soinside.com 2019 - 2024. All rights reserved.