【问题标题】:How to handle data manipulation when using importrange() in Google Sheets?在 Google 表格中使用 importrange() 时如何处理数据操作?
【发布时间】:2021-07-27 05:49:45
【问题描述】:

我正在努力加快使用 importrange() 的 Google 表格中的工作簿。整个工作簿的目的是从母版表中导入数据,然后允许我们在母版表之外以我们想要的方式对其进行操作。

问题:因为 importrange() 不允许您直接操作单元格,所以我们将 Sheet1 用作导入表;它不会被触动。 Sheet2 是我们进行操作的地方,但实际上它只是作为 Sheet1 的副本,所以它也使用 importrange()。这会使整个工作簿陷入困境并使操作变得非常缓慢。

我正在考虑使用 !Sheet1A1... 并将其复制到操作表中的所有单元格,但我担心这仍然会使工作簿陷入困境。导入数据有可能增长到 10k+ 行,而我目前只有大约一半并且遇到了这个问题。除此之外,我不确定还有什么可以尝试的。

【问题讨论】:

    标签: google-sheets google-sheets-formula


    【解决方案1】:

    QUERY 功能可以在这里提供帮助,网上有一些很棒的资源。

    =importrange(spreadsheet_url, range_string)

    一个典型的例子是:

    =importrange("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxx","Sheet1!A:Z")

    您可以围绕它包装一个QUERY 函数来操作您的数据。

    QUERY 就像 SQL 的一个版本,非常强大。格式为:

    =QUERY({},"",1)

    您的数据范围importrange("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxx","Sheet1!A:Z") 将在{} 范围内。

    然后在查询的"" 部分中,您可以编写用于操作数据的参数。

    例子:

    select Col1,Col4,Col5 where Col1 is not null and Col6 contains 'hello' order by Col1,Col7 desc label Col1 'new name 1',Col4 'new name 4'

    select 位允许您指定导入范围中的特定列。如果你想要全部,那么你可以使用select *

    where 项目是您使用各种 orand 参数建立标准的地方。

    is not null 是另一种表示您想要包含数据的行的方式。

    contains 很有用。您还可以拥有matchesstarts withends withlikelike 可以使用通配符%,所以where Col1 like '%the%' 会找到'hello there'。

    order by 是升序的,除非你添加desc,即。 order by Col1,Col2,Col4,Col5 desc,Col3.

    label 允许您重命名列,因此假设输入列 1 称为“Name1”,输入列 2 为“Name2”,您希望它们为“First name”和“Surname”,您可以使用 @ 987654348@.

    如果您喜欢QUERY,还有其他强大的子句,它们在QUERY(range,"clauses",0) 内按此顺序运行:

    select

    where

    group by

    pivot

    order by

    limit

    offset

    label

    format

    options

    您可能会遇到一个小问题,当您使用 importrange 获取数据时,您需要在 QUERY 中将列引用为 Col1,Col2,Col3

    但是,如果您的范围已经在同一个工作表中(相同或不同的选项卡),那么您将改为引用列字母,例如。 select A,B,C where A is not null order by A desc.

    为了使其更加一致并使用Col1,Col2,Col3 表示法,您可以将内部范围放入数组{}

    QUERY(Sheet1!B:F,"select B,C,D where F is not null order by B,C",0)

    会变成:

    QUERY({Sheet1!B:F},"select Col1,Col2,Col3 where Col5 is not null order by Col1,Col2",0)

    {Sheet1!B:F} 很聪明,因为您可以在此范围之前添加列,而无需更改子句。因此,在 Sheet1 前面添加一列,将导致:

    QUERY({Sheet1!C:G},"select Col1,Col2,Col3 where Col5 is not null order by Col1,Col2",0)

    另一种方法需要您更改您的子句:

    QUERY(Sheet1!B:F,"select B,C,D where F is not null order by B,C",0)

    到:

    QUERY(Sheet1!C:G,"select C,D,E where G is not null order by C,D",0)

    有很多东西可以吸收,但绝对值得说服!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      相关资源
      最近更新 更多