简体   繁体   English

如何使用Google Java Sheet API V4将单元格格式化为NUMBER和DATE

[英]How to use Google Java Sheet API V4 to format cells as NUMBER and DATE

Greeting, 问候,

I'm trying to add formatting to my Google Sheets using the Java API V4. 我正在尝试使用Java API V4向我的Google表格添加格式。

The following code is my best understanding of what is required to format a series of columns and rows to be of type NUMBER with a single integer digit. 以下代码是我最好的理解将一系列列和行格式化为NUMBER类型和一个整数位所需的内容。 Also there is one column with a DATE format. 还有一列具有DATE格式。 The code does not return any errors buts also does not alter the formatting on my sheet such that eash numeric values and date have a annoying apostrophe in front of each value like '5 and 'Oct 26 2016 13:34. 代码不会返回任何错误,但也不会改变我的工作表上的格式,使得eash数值和日期在每个值前面都有一个恼人的撇号,如'5和'2016年10月26日13:34。 I'm been trying to format the fields in order to remove that apostrophe which the sheet is adding automatically. 我一直在尝试格式化字段以删除工作表自动添加的撇号。

Any ideas what I'm doing wrong? 我有什么想法我做错了吗?

    public static void setProperties(String spreadsheetId, int length) throws IOException {


    List<Request> requests = new ArrayList<>();

    requests.add(new Request()
            .setRepeatCell(new RepeatCellRequest()
                    .setCell(new CellData()
                            .setUserEnteredFormat(new CellFormat().setNumberFormat(new NumberFormat().setPattern("0").setType("NUMBER"))))
                    .setRange(new GridRange()
                            .setSheetId(0)
                            .setStartRowIndex(3)
                            .setEndRowIndex(2 + length)
                            .setStartColumnIndex(3)
                            .setEndColumnIndex(22))
                    .setFields("userEnteredFormat.numberFormat")
            ));
    requests.add(new Request()
            .setRepeatCell(new RepeatCellRequest()
                    .setCell(new CellData()
                            .setUserEnteredFormat(new CellFormat().setNumberFormat(new NumberFormat().setPattern("0").setType("NUMBER"))))
                    .setRange(new GridRange()
                            .setSheetId(0)
                            .setStartRowIndex(3)
                            .setEndRowIndex(2 + length)
                            .setStartColumnIndex(24)
                            .setEndColumnIndex(24))
                    .setFields("userEnteredFormat.numberFormat")
            ));
    requests.add(new Request()
            .setRepeatCell(new RepeatCellRequest()
                    .setCell(new CellData()
                            .setUserEnteredFormat(new CellFormat().setNumberFormat(new NumberFormat().setPattern("mmm dd yyyy hh+:mm").setType("DATE"))))
                    .setRange(new GridRange()
                            .setSheetId(0)
                            .setStartRowIndex(3)
                            .setEndRowIndex(2 + length)
                            .setStartColumnIndex(23)
                            .setEndColumnIndex(23))
                    .setFields("userEnteredFormat.numberFormat")
            ));

    BatchUpdateSpreadsheetRequest batchUpdateRequest = new BatchUpdateSpreadsheetRequest()
            .setRequests(requests);
    SHEETS.spreadsheets().batchUpdate(spreadsheetId, batchUpdateRequest)
            .execute();
}




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

Thanks, Conteh 谢谢,Conteh

Check this question Google Sheet API V4(Java) append Date in cells , where a similar issue is reported. 检查此问题Google Sheet API V4(Java)在单元格中附加日期 ,其中报告了类似的问题。

Additionally, I've used Google Sheet API V3 and Google Sheet API V4 that issue is not present in v3. 此外,我使用了Google Sheet API V3和Google Sheet API V4,但v3中不存在此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM