【问题标题】:Real time clock in Google sheetsGoogle 表格中的实时时钟
【发布时间】:2017-11-03 21:13:34
【问题描述】:

有没有办法在谷歌表格中输出实时时钟值?基本上我要做的是使用单元格中的值创建一个实时甘特图,该图将根据时间值和操作员使用的停机原因输出机器的状态。

【问题讨论】:

  • 我正在致力于自动化数据输入。我只需要电子表格每 5 到 10 分钟更新一次,并根据时间输出一个值。这个程序可能不可行。我对 Microsoft 访问做了类似的事情。

标签: google-sheets


【解决方案1】:

有两种方法可以实现这一点。第一个是最简单的,=now() 每分钟自动更新一次。第二种技术更复杂,但提供了更多的控制,特别是如果您想控制时钟应该更新的精确时刻。

最简单的方法 - 20 秒:

  • 应用 =NOW() 函数并在文件 > 电子表格设置中的电子表格中将重新计算设置更改为“每分钟更改一次”。

漫长的路 - 5 到 30 分钟。要求服务器每 x 分钟执行一次代码

可以通过 API 来实现。有几种方法可以实现这一点,但都涉及 API。

1) 创建一张空白表并使用 API 更新任何单元格中的时间。让我们将此表称为通用。只需将“=now()”插入单元格将始终确保每次进行 API 调用时都会更新时间。在每个 API 调用中用“=now()”覆盖这个单元格。然后在引用 universal 表中的调用值的任何表中使用函数:IMPORTRANGE()。这是首选选项,因为您只需为一张工作表创建代码,然后您就可以在任何其他工作表中引用。

2) 与上述相同,但只需直接写入您的工作表和单元格,而不是 通用 工作表。这种方法的缺点是,如果您移动单元格,您将需要更改您的代码。

这两个选项都要求您调用 API 并设置计划作业以每 x 分钟运行一次程序。

说明(JAVA - 尽管您可以对其他语言遵循相同的逻辑)。下面的大部分内容都是从 Google Sheets API 指南复制而来的,我在下面列出了我的所有步骤:

第 1 步:开启 Google Sheets API

a 使用this wizard 在 Google Developers Console 中创建或选择项目并自动开启 API。单击继续,然后转到凭据。

b在将凭据添加到您的项目页面上,单击取消按钮。

c 在页面顶部,选择 OAuth 同意屏幕选项卡。选择一个电子邮件地址,输入产品名称(如果尚未设置),然后单击“保存”按钮。

d 选择凭据选项卡,单击创建凭据按钮并选择 OAuth 客户端 ID。

e选择应用类型Other,输入名称“Google Sheets API Quickstart”,点击创建按钮。

f单击“确定”关闭生成的对话框。

g点击客户端 ID 右侧的 file_download(下载 JSON)按钮。

h 将此文件移动到您的工作目录并将其重命名为 client_secret.json。

第 2 步如果使用 Maven,请将以下依赖项添加到您的 pom 文件中:

<dependency>
     <groupId>com.google.api-client</groupId>
     <artifactId>google-api-client-java6</artifactId>
     <version>1.23.0</version>
</dependency>

 <dependency>
     <groupId>com.google.oauth-client</groupId>
     <artifactId>google-oauth-client-jetty</artifactId>
     <version>1.23.0</version>
 </dependency>

<dependency>
     <groupId>com.google.apis</groupId>
     <artifactId>google-api-services-sheets</artifactId>
     <version>v4-rev504-1.23.0</version>
</dependency>

第三步复制这个类Credentials.java

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;

public class Credentials {

   /** Application name. */
private static final String APPLICATION_NAME =
    "Google Sheets";

/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
    System.getProperty("user.home"), ".credentials/sheets.googleapis.com-java-quickstart");

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =
    JacksonFactory.getDefaultInstance();

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/** Global instance of the scopes required by this quickstart.
 *
 * If modifying these scopes, delete your previously saved credentials
 * at ~/.credentials/sheets.googleapis.com-java-quickstart
 */
private static final List<String> SCOPES =
    Arrays.asList(SheetsScopes.SPREADSHEETS);

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in =
        Quickstart.class.getResourceAsStream("/client_secret.json");
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

/**
 * Build and return an authorized Sheets API client service.
 * @return an authorized Sheets API client service
 * @throws IOException
 */
public static Sheets getSheetsService() throws IOException {
    Credential credential = authorize();
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

}

第 4 步 复制下面的 UpdateDate.java 类。将电子表格 ID 更改为电子表格的 ID。使用您的单元格参考更改范围。我已经输入了默认值 Sheet1!a1 作为例子

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
import com.google.api.services.sheets.v4.model.ValueRange;

public class UpdateDate {

public static void main(String[] args) throws IOException{

    Sheets service = Credentials.getSheetsService();

    String spreadsheetId = "YOUR_SHEET_ID";
    String range = "Sheet1!a1";


    @SuppressWarnings("unchecked")
    List<List<Object>> values = Arrays.asList(
            Arrays.asList(
                    (Object)"=NOW()"
            )
    );

    ValueRange body = new ValueRange().setValues(values);


        UpdateValuesResponse result =
                service.spreadsheets().values().update(spreadsheetId, range, body)
                        .setValueInputOption("USER_ENTERED")
                        .execute();


}

}

第 5 步根据您的用例设置一个计划作业,使其每 x 分钟运行一次。第一次运行时,您的浏览器应该会打开,您需要点击接受同意屏幕。然后将凭据保存在本地,这样您就可以通过命令行运行该过程,而无需再次单击同意屏幕。

祝你好运!

【讨论】:

    【解决方案2】:

    看起来你想要NOW 函数。我认为工作表不支持实时更新,因此最好使用 volatile 函数(更新工作表时更改)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多