【发布时间】:2020-02-05 12:39:58
【问题描述】:
我正在尝试更新/替换 Google 文档 模板中的给定文本。
到目前为止我尝试过的代码:
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)
我错过了什么/做错了什么?
【问题讨论】:
-
愿意打赌那不是文件 ID。从 google drive api 执行 files.list 并找到正确的文件 ID。还有什么在 GetDriveService
-
添加了files.list请求,表示给定的fileId
-
哪一行在未找到的东西上失败了。
-
方法
BatchUpdateRequest只需要两个参数 - 请求和文档 ID,请参阅 here 以获取示例。 -
第三个参数是OPTIONAL字段
writeControl。唯一需要的参数是documentId和requests。我不知道您的变量service是什么,但它不太可能是必需的参数之一。我假设它是您想要应用批处理请求的resource。那么它应该类似于service.BatchUpdateRequest(body, "160NinGjrmshSga8fWkCFRwApV0FTL1BiJCidH7A1yFw");我建议您在 C# 实现之前使用 Try it API 测试您的请求
标签: c# google-api google-docs