本文介绍了C#批量更新文档请求替换所有文本Google Docs Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更新/替换Google Docs模板中的给定文本。
我到目前为止尝试过的代码:
DriveService service = GetDriveService();
var firstname = "Firstname";
var lastname = "Lastname";
BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest();
List<Request> requests = new List<Request>();
var repl = new Request();
var substrMatchCriteria = new SubstringMatchCriteria();
var replaceAlltext = new ReplaceAllTextRequest();
replaceAlltext.ReplaceText = firstname + "." + lastname;
substrMatchCriteria.Text = "vorname.nachname";
replaceAlltext.ContainsText = substrMatchCriteria;
repl.ReplaceAllText = replaceAlltext;
requests.Add(repl);
body.Requests = requests;
//Batch update Request requires 3 Parameters
DocumentsResource.BatchUpdateRequest bUpdate = new DocumentsResource.BatchUpdateRequest(service, body, "160NinGjrmshSga8fWkCFRwApV0FTL1BiJCidH7A1yFw");
bUpdate.Execute(); // The Exception is raised here
DocumentsResource.BatchUpdateRequest
需要以下参数:
出现以下错误:
Google.GoogleApiException: "Not Found"
JsonReaderException: Error parsing NaN value. Path '', line 1, position 1.
Diese Ausnahme wurde ursprünglich bei dieser Aufrufliste ausgelöst:
Newtonsoft.Json.JsonTextReader.ParseNumberNaN(Newtonsoft.Json.ReadType, bool)
Newtonsoft.Json.JsonTextReader.ParseValue()
Newtonsoft.Json.JsonTextReader.Read()
Newtonsoft.Json.JsonReader.ReadForType(Newtonsoft.Json.Serialization.JsonContract, bool)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.JsonReader, System.Type, bool)
Newtonsoft.Json.JsonSerializer.DeserializeInternal(Newtonsoft.Json.JsonReader, System.Type)
Newtonsoft.Json.JsonConvert.DeserializeObject(string, System.Type, Newtonsoft.Json.JsonSerializerSettings)
Google.Apis.Services.BaseClientService.DeserializeError(System.Net.Http.HttpResponseMessage)
我从文档路径获取了DocumentID
还使用文件列表显示该文件:
我错过了什么/做错了什么?
推荐答案
首先,您可以尝试泛化GetService方法,我这样做是因为我使用了各种作用域
public static object GetService(apiType api = apiType.GDrive)
{
UserCredential credential;
string CSPath = HostingEnvironment.MapPath("~/MyFolder/");
using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read))
{
string FolderPath = CSPath;
string FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)
).Result;
}
if (api == apiType.GDocs)
{
DocsService docsService = new DocsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = MyApplicationName,
});
return docsService;
}
else
{
DriveService driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = MyApplicationName
});
return driveService;
}
}
然后您可以尝试执行以下操作:
public static void EditDoc(string fileId, Dictionary<string, string> textToReplace)
{
DocsService service = (DocsService)GetService(apiType.GDocs);
List<Request> requests = new List<Request>();
foreach (var text in textToReplace)
{
var repl = new Request();
var substrMatchCriteria = new SubstringMatchCriteria();
var replaceAlltext = new ReplaceAllTextRequest();
replaceAlltext.ReplaceText = text.Value;
substrMatchCriteria.Text = text.Key;
replaceAlltext.ContainsText = substrMatchCriteria;
repl.ReplaceAllText = replaceAlltext;
requests.Add(repl);
}
BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest { Requests = requests };
service.Documents.BatchUpdate(body, fileId).Execute();
}
我将本文档作为参考https://developers.google.com/docs/api/how-tos/merge由于某种原因,Google没有将其翻译成C#...
这篇关于C#批量更新文档请求替换所有文本Google Docs Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!