Google 스프레드 시트가 있습니다. 내 E 칼럼에서 최근 칼럼을 올해와 함께 추가하려고합니다 (예 : Sep 2017). E 칼럼에서 사용할 수없는 경우. 2017 년 8 월에 추가하고 (2017 년 9 월) 추가 할 때 기존 칼럼을 대체합니다. 내 요구 사항은 새로 추가 된 것 (2017 년 9 월)과 (2017 년 8 월) 둘 다 유지하는 것입니다. 나는 최근 한 달간 E 칼럼을 원해서 오른쪽 칼럼 인 기존 칼럼을 옮긴다. C#을 사용하여이 작업을 수행하고 있습니다.Google 스프레드 시트 Api를 사용하여 두 열 사이에 열을 추가 할 수 없습니다.
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = GetCredential(),
ApplicationName = ApplicationName,
});
String spreadsheetId = "1lZnvQe6lTGG81hyuQvf7HjH8YpnIadFlNHjFUq_G-5Q";
String range = "E1";
SpreadsheetsResource.ValuesResource.GetRequest getRequest = service.Spreadsheets.Values.Get(spreadsheetId, range);
Data.ValueRange response = getRequest.Execute();
string recentMonth = DateTime.Now.ToString("MMM yyyy");
var recentMonthArrayValue = new List<string[]>();
recentMonthArrayValue.Add(new string[] { recentMonth });
if (response.Values[0][0].ToString() != recentMonth)
{
DateTime now = DateTime.Now;
var currentMonthStartDate = new DateTime(now.Year, now.Month, 1).ToShortDateString();
Data.BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new Data.BatchUpdateSpreadsheetRequest();
batchUpdateSpreadsheetRequest.Requests = new List<Data.Request>();
Data.Request request = new Data.Request();
batchUpdateSpreadsheetRequest.Requests.Add(request);
request.UpdateCells = new Data.UpdateCellsRequest();
var gridCoordinate = new Data.GridCoordinate();
gridCoordinate.ColumnIndex = 5;
gridCoordinate.SheetId = 0;
request.UpdateCells.Start = gridCoordinate;
request.UpdateCells.Fields = "*";
request.UpdateCells.Rows = new List<Data.RowData>();
var rowData = new Data.RowData();
request.UpdateCells.Rows.Add(rowData);
rowData.Values = new List<Data.CellData>();
var cellData = new Data.CellData();
cellData.UserEnteredValue = new Data.ExtendedValue();
cellData.UserEnteredValue.FormulaValue = "=TEXT(\"" + currentMonthStartDate + "\",\"MMM yyyy\")";
rowData.Values.Add(cellData);
SpreadsheetsResource.BatchUpdateRequest batchUpdateRequest = service.Spreadsheets.BatchUpdate(batchUpdateSpreadsheetRequest, spreadsheetId);
batchUpdateRequest.Execute();
}
}
catch (Exception e)
{
//throw;
}
}
public static UserCredential GetCredential()
{
UserCredential credential;
using (var stream =new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
return credential;
}
}
가 그래 난 그것을 알고,하지만 당신은 C#을 함께 할 방법을 알고 .If 내가 C#을 함께 할 수없는 나는, 말해. – test