我正在尝试在电子表格中创建一个工作表,该工作表是现有工作表的副本。通过Google文档用户界面,它通过工作表上下文菜单的"复制"功能完成。是否可以一次复制整个工作表并将其插入另一个工作表?此代码:
public void CopyLastTab(SpreadsheetEntry spreadsheet, string newTabName)
{
WorksheetEntry tabToCopy = GetLastTab(spreadsheet);
tabToCopy.Title.Text = newTabName;
newTab = service.Insert(GetAllWorksheets(spreadsheet), tabToCopy);
}
仅添加新的空工作表。逐个单元格复制太长,问题是我也需要在上一张纸上使用条件格式。请帮忙。
老问题,但只是花了几个小时试图找到答案。以下内容适用于 C# .net 6:
请注意,这假设您有一个 Google 表格服务的实例(Google.APIs.Sheets.v4) 已通过身份验证。我将在下面发布一些身份验证代码。
public void Duplicate(int SourceSheetId, string NewSheetName) {
var batch = new BatchUpdateSpreadsheetRequest();
batch.Requests = new List<Request>();
batch.Requests.Add(new Request() {
DuplicateSheet = new DuplicateSheetRequest() {
NewSheetName = NewSheetName,
SourceSheetId = SourceSheetId
},
});
var req = Service.Spreadsheets.BatchUpdate(batch, SheetID); //public SheetsService Service; property of parent class
var response = req.Execute();
}
private void _authenticate()
{
UserCredential credential;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.ReadWrite))
{
string credPath = "token.json";
var cancelToken = new CancellationToken();
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
_scopes,
YOUR-EMAIL-HERE,
cancelToken,
new FileDataStore(credPath, true)).Result;
//credential.RevokeTokenAsync(cancelToken); //UNCOMMENT TO LOG OUT
}
Service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "",
});
}