Gau*_*ara 6 java google-sheets google-spreadsheet-api google-sheets-api
我试图在单元格中添加日期,但工作表会自动将值存储在带有单引号(')的字符串中.对于日期中的商店价值,我们也尝试添加,userEnteredFormat但它对我们不起作用.
以下是追加请求.
{
requests = [{
appendCells = {
fields = userEnteredValue,
userEnteredFormat.numberFormat,
rows = [{
values = [{
userEnteredValue = {
numberValue = 10.0
}
}, {
userEnteredValue = {
stringValue = Sample String
}
}, {
userEnteredFormat = {
numberFormat = {
type = DATE
}
},
userEnteredValue = {
stringValue = 2015 - 07 - 13
}
}, {
userEnteredValue = {
boolValue = true
}
}, {
userEnteredFormat = {
numberFormat = {
type = DATE
}
},
userEnteredValue = {
stringValue = 2015 - 07 - 13
}
}]
}],
sheetId = abc
}
}]}
Run Code Online (Sandbox Code Playgroud)
示例代码用于在工作表上附加单个日期单元格
package org.pentaho.googlesheets.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.pdi.steps.googlesheets.GoogleSheetsOutputStepMeta;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.gson.GsonFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.AppendCellsRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.CellData;
import com.google.api.services.sheets.v4.model.CellFormat;
import com.google.api.services.sheets.v4.model.ExtendedValue;
import com.google.api.services.sheets.v4.model.NumberFormat;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.RowData;
public class DateIssueSample {
static String APPLICATION_NAME ;
static JsonFactory JSON_FACTORY;
static HttpTransport HTTP_TRANSPORT;
static List<String> SPREADSHEET_SCOPES ;
static List<String> DRIVE_SCOPES ;
static Sheets service;
static String email = "demo-983@praxis-practice-133423.iam.gserviceaccount.com";
static String pkey ="E:\\P12Key\\My Project-834a8d37d247.p12";
public static Credential authorize(List<String> SCOPES ) throws Exception {
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(email)
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new java.io.File(pkey))
.build();
credential.refreshToken();
return credential;
}
public static Sheets getSheetsService() throws Exception {
Credential credential = authorize(SPREADSHEET_SCOPES);
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public static void main(String[] args) throws Exception {
APPLICATION_NAME = "PDI";
JSON_FACTORY =new GsonFactory();
SPREADSHEET_SCOPES =Arrays.asList(SheetsScopes.SPREADSHEETS);
DRIVE_SCOPES=Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
service = getSheetsService();
String spreadSheetID= "abc";
Integer sheetID = 123;
String DateValue = "2015-07-13";
List<RowData> rowData = new ArrayList<RowData>();
List<CellData> cellData = new ArrayList<CellData>();
CellData cell = new CellData();
cell.setUserEnteredValue(new ExtendedValue().setStringValue(DateValue));
cell.setUserEnteredFormat(new CellFormat().setNumberFormat(new NumberFormat().setType("DATE")));
cellData.add(cell);
rowData.add(new RowData().setValues(cellData));
BatchUpdateSpreadsheetRequest batchRequests = new BatchUpdateSpreadsheetRequest();
BatchUpdateSpreadsheetResponse response;
List<Request> requests = new ArrayList<Request>();
AppendCellsRequest appendCellReq = new AppendCellsRequest();
appendCellReq.setSheetId( sheetID);
appendCellReq.setRows( rowData );
appendCellReq.setFields("userEnteredValue,userEnteredFormat.numberFormat");
requests = new ArrayList<Request>();
requests.add( new Request().setAppendCells(appendCellReq));
batchRequests = new BatchUpdateSpreadsheetRequest();
batchRequests.setRequests( requests );
response= service.spreadsheets().batchUpdate(spreadSheetID, batchRequests).execute();
System.out.println("Request \n\n");
System.out.println(batchRequests.toPrettyString());
System.out.println("\n\nResponse \n\n");
System.out.println(response.toPrettyString());
}
}
Run Code Online (Sandbox Code Playgroud)
要提供Sam的答案意味着什么的示例,如果您只想使用创建日期值AppendCellsRequest,您可以像这样创建单元格:
CellData cell = new CellData();
cell.setUserEnteredValue(new ExtendedValue().setNumberValue(42198.0));
cell.setUserEnteredFormat(
new CellFormat().setNumberFormat(new NumberFormat().setType("DATE")));
Run Code Online (Sandbox Code Playgroud)
这里42198是1899年12月30日到2015年7月13日之间的天数.
请参阅API 如何处理日期时间的介绍指南说明。
表格中的日期是数字,而不是字符串。(例如,这让您可以对它们进行算术运算。)
如果使用'values' 集合,则有用于从字符串转换为日期的钩子,反之亦然(使用不同的ValueInputOptions或ValueRenderOptions。
不幸的是,值集合中还没有 Append 方法。因此,要轻松地在现有数据后附加单元格,您需要使用电子表格.batchUpdate,而这只是原始电子表格 DOM。所以现在,您需要输入日期作为序列号(带有日期格式),如第一个链接中所述。
使用单引号添加字符串的原因是因为您告诉 API 您要添加字符串,因此要添加引号以防止 Sheets 意外将值解析为日期。
| 归档时间: |
|
| 查看次数: |
4694 次 |
| 最近记录: |