我的应用程序当前能够将所有数据从Excel电子表格中的指定工作表传输到Google表格,并使用ClearValuesRequest
清除Google表格中的指定工作表中的所有值。
如何清除Google Spreadsheet中指定工作表的所有格式(字体样式,单元格颜色等)?我花了数小时来浏览documentation,但由于无法像使用ClearValuesRequest
一样简单明了,因此无法确切地了解我需要做什么。
该代码的作用范围仅是清除电子表格中的值。
public string ClearSheetData(string spreadsheetId, string sheetName)
{
try
{
GoogleConnections googleConnections = new GoogleConnections();
new ConnectToGoogle().ConnectToGoogleSheets(googleConnections, ClientSecretFilePath, ApplicationName, UserName);
sheetName = sheetName.Replace("!", "").Replace("$","");
sheetName = string.Concat(sheetName, "!").TrimEnd();
string range = string.Concat(sheetName, "A:ZZ");
ClearValuesRequest requestBody = new ClearValuesRequest();
SpreadsheetsResource.ValuesResource.ClearRequest clearRequest =
googleConnections.sheetsService.Spreadsheets.Values.Clear(requestBody, spreadsheetId, range);
clearRequest.Fields = "*"; //I had hoped that this would clear the formats as well. But it seems to be used as a selector for a partial response.
ClearValuesResponse response = clearRequest.Execute();
return (JsonConvert.SerializeObject(response));
}
catch (Exception e)
{
return string.Concat("Message: ", e.Message, Environment.NewLine, "StackTrace: ", e.StackTrace, Environment.NewLine, "InnerException: ", e.InnerException);
}
}
答案 0 :(得分:0)
要回答我自己的问题,这对我有用。如果有/有更好的方法,那么我不知道。
经过大量研究,我找不到使用ClearValuesRequest
清除任何格式的选项。我不得不执行BatchUpdateSpreadsheetRequest
。从那里,我不得不使用RepeatCellRequest
并为GridRange
提供一个Range
来重复该单元格。不幸的是GridRange
不接受A1表示法,因此我不得不创建Helper方法以将A1表示法的范围转换为索引。
这将清除/重置整个工作表的所有格式。
ClearFormatting方法
public string ClearFormatting(SheetsService sheetsService, string spreadsheetId, string sheetName, string range)
{
string str = string.Empty;
try
{
GoogleSheetsHelper helper = new GoogleSheetsHelper();
range = helper.RemoveSheetNameHelper(range);
string[] strArray = range.Split(':');
List<Request> requestList = new List<Request>();
BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest()
{
Requests = (IList<Request>)requestList
};
int sheetId = helper.GetSheetIdHelper(sheetsService, spreadsheetId, sheetName);
Request request = new Request()
{
RepeatCell = new RepeatCellRequest()
{
Range = new GridRange()
{
SheetId = new int?(sheetId),
StartColumnIndex = new int?(GoogleSheetsHelper.A1ToColumnHelper(strArray[0]) - 1),
StartRowIndex = new int?(GoogleSheetsHelper.A1ToRowHelper(strArray[0]) - 1),
},
Fields = "*"
}
};
body.Requests.Add(request);
BatchUpdateSpreadsheetResponse response = sheetsService.Spreadsheets.BatchUpdate(body, spreadsheetId).Execute();
str = JsonConvert.SerializeObject(response);
}
catch (Exception e)
{
str = "Message" + e.Message + Environment.NewLine + Environment.NewLine + "InnerException: " + e.InnerException + Environment.NewLine + Environment.NewLine + "Data: " + e.Data + Environment.NewLine + Environment.NewLine + "HelpLink: " + e.HelpLink + Environment.NewLine + Environment.NewLine + "Source: " + e.Source + Environment.NewLine + Environment.NewLine + "StackTrace: " + e.StackTrace + Environment.NewLine + Environment.NewLine + "TargetSite: " + e.TargetSite + Environment.NewLine + Environment.NewLine;
}
return str;
}
A1ToRowHelper
internal static int A1ToRowHelper(string a1)
{
string empty = string.Empty;
string str = a1;
for (int i = 0; i < str.Length; i++)
{
char chr = str[i];
if (char.IsNumber(chr))
{
empty = string.Concat(empty, chr.ToString());
}
}
return int.Parse(empty);
}
A1ToColumnHelper
internal static int A1ToColumnHelper(string a1)
{
string empty = string.Empty;
string str = a1;
for (int i = 0; i < str.Length; i++)
{
char chr = str[i];
if (char.IsLetter(chr))
{
empty = string.Concat(empty, chr.ToString());
}
}
if (string.IsNullOrEmpty(a1))
{
throw new ArgumentNullException("a1");
}
empty = empty.ToUpperInvariant();
int num = 0;
for (int i = 0; i < empty.Length; i++)
{
char chr1 = empty[i];
num *= 26;
num = num + chr1 - 65 + 1;
}
return num;
}
GetSheetIdHelper
public int GetSheetIdHelper(SheetsService sheetsService, string spreadsheetId, string sheetName)
{
Sheet sheet = sheetsService.Spreadsheets.Get(spreadsheetId).Execute().Sheets.First<Sheet>((Sheet x) => x.Properties.Title.Equals(sheetName));
int? sheetId = sheet.Properties.SheetId;
return (sheetId.HasValue ? sheetId.GetValueOrDefault() : 0);
}