当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R googlesheets4 gs4_formula Google 表格公式类


为了将公式写入 Google Sheets,您需要将其存储为类 googlesheets4_formula 的对象。这就是我们如何区分 "regular" 字符串和应解释为公式的字符串。 googlesheets4_formula 是使用 vctrs package 实现的 S3 类。

用法

gs4_formula(x = character())

参数

x

特点。

googlesheets4_formula 的 S3 向量。

也可以看看

其他写入函数:gs4_create()range_delete()range_flood()range_write()sheet_append()sheet_write()

例子

dat <- data.frame(x = c(1, 5, 3, 2, 4, 6))

ss <- gs4_create("gs4-formula-demo", sheets = dat)
#> ✔ Creating new Sheet: gs4-formula-demo.
ss
#> 
#> ── <googlesheets4_spreadsheet> ───────────────────────────────────────────
#> Spreadsheet name: gs4-formula-demo                            
#>               ID: 13ZWDvjoCtLz1lauqq2eZQBTXOpWXo5YTSVFEPaHsdsw
#>           Locale: en_US                                       
#>        Time zone: Etc/GMT                                     
#>      # of sheets: 1                                           
#> 
#> ── <sheets> ──────────────────────────────────────────────────────────────
#> (Sheet name): (Nominal extent in rows x columns)
#>          dat: 7 x 1

summaries <- tibble::tribble(
  ~desc, ~summaries,
  "max", "=max(A:A)",
  "sum", "=sum(A:A)",
  "min", "=min(A:A)",
  "sparkline", "=SPARKLINE(A:A, {\"color\", \"blue\"})"
)

# explicitly declare a column as `googlesheets4_formula`
summaries$summaries <- gs4_formula(summaries$summaries)
summaries
#> # A tibble: 4 × 2
#>   desc      summaries                         
#>   <chr>     <fmla>                            
#> 1 max       =max(A:A)                         
#> 2 sum       =sum(A:A)                         
#> 3 min       =min(A:A)                         
#> 4 sparkline =SPARKLINE(A:A, {"color", "blue"})

range_write(ss, data = summaries, range = "C1", reformat = FALSE)
#> ✔ Editing gs4-formula-demo.
#> ✔ Writing to sheet dat.
#> ✔ Changing dims: (7 x 1) --> (7 x 4).

miscellany <- tibble::tribble(
  ~desc, ~example,
  "hyperlink", "=HYPERLINK(\"http://www.google.com/\",\"Google\")",
  "image", "=IMAGE(\"https://www.google.com/images/srpr/logo3w.png\")"
)
miscellany$example <- gs4_formula(miscellany$example)
miscellany
#> # A tibble: 2 × 2
#>   desc      example                                                
#>   <chr>     <fmla>                                                 
#> 1 hyperlink =HYPERLINK("http://www.google.com/","Google")          
#> 2 image     =IMAGE("https://www.google.com/images/srpr/logo3w.png")

sheet_write(miscellany, ss = ss)
#> ✔ Writing to gs4-formula-demo.
#> ✔ Writing to sheet miscellany.

# clean up
gs4_find("gs4-formula-demo") %>%
  googledrive::drive_trash()
#> File trashed:
#> • gs4-formula-demo <id: 13ZWDvjoCtLz1lauqq2eZQBTXOpWXo5YTSVFEPaHsdsw>
源代码:R/gs4_formula.R

相关用法


注:本文由纯净天空筛选整理自Jennifer Bryan等大神的英文原创作品 Class for Google Sheets formulas。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。