使用Google JS api 创建 文档

举报
拿我格子衫来 发表于 2022/03/18 00:47:21 2022/03/18
【摘要】 需要配置 ApiKey 与YOUR_CLIENT_ID <script src="https://apis.google.com/js/api.js"></script><script> /** * Sample JavaScript code for docs.documents....

需要配置 ApiKey 与YOUR_CLIENT_ID


  
  1. <script src="https://apis.google.com/js/api.js"></script>
  2. <script>
  3. /**
  4. * Sample JavaScript code for docs.documents.create
  5. * See instructions for running APIs Explorer code samples locally:
  6. * https://developers.google.com/explorer-help/guides/code_samples#javascript
  7. */
  8. function authenticate() {
  9. return gapi.auth2.getAuthInstance()
  10. .signIn({scope: "https://www.googleapis.com/auth/documents https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file"})
  11. .then(function() { console.log("Sign-in successful"); },
  12. function(err) { console.error("Error signing in", err); });
  13. }
  14. function loadClient() {
  15. gapi.client.setApiKey("YOUR_API_KEY");
  16. return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/docs/v1/rest")
  17. .then(function() { console.log("GAPI client loaded for API"); },
  18. function(err) { console.error("Error loading GAPI client for API", err); });
  19. }
  20. // Make sure the client is loaded and sign-in is complete before calling this method.
  21. function execute() {
  22. return gapi.client.docs.documents.create({
  23. "resource": {}
  24. })
  25. .then(function(response) {
  26. // Handle the results here (response.result has the parsed body).
  27. console.log("Response", response);
  28. },
  29. function(err) { console.error("Execute error", err); });
  30. }
  31. gapi.load("client:auth2", function() {
  32. gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  33. });
  34. </script>
  35. <button onclick="authenticate().then(loadClient)">authorize and load</button>
  36. <button onclick="execute()">execute</button>

 

 

 

 

 

 


  
  1. <script src="https://apis.google.com/js/api.js"></script>
  2. <script>
  3. /**
  4. * Sample JavaScript code for docs.documents.batchUpdate
  5. * See instructions for running APIs Explorer code samples locally:
  6. * https://developers.google.com/explorer-help/guides/code_samples#javascript
  7. */
  8. function authenticate() {
  9. return gapi.auth2.getAuthInstance()
  10. .signIn({scope: "https://www.googleapis.com/auth/documents https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file"})
  11. .then(function() { console.log("Sign-in successful"); },
  12. function(err) { console.error("Error signing in", err); });
  13. }
  14. function loadClient() {
  15. gapi.client.setApiKey("YOUR_API_KEY");
  16. return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/docs/v1/rest")
  17. .then(function() { console.log("GAPI client loaded for API"); },
  18. function(err) { console.error("Error loading GAPI client for API", err); });
  19. }
  20. // Make sure the client is loaded and sign-in is complete before calling this method.
  21. function execute() {
  22. return gapi.client.docs.documents.batchUpdate({
  23. "resource": {}
  24. })
  25. .then(function(response) {
  26. // Handle the results here (response.result has the parsed body).
  27. console.log("Response", response);
  28. },
  29. function(err) { console.error("Execute error", err); });
  30. }
  31. gapi.load("client:auth2", function() {
  32. gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  33. });
  34. </script>
  35. <button onclick="authenticate().then(loadClient)">authorize and load</button>
  36. <button onclick="execute()">execute</button>

使用 batchUpdate 更新文档

 

传入 resource  一个json对象 参数如下

https://developers.google.com/docs/api/reference/rest/v1/documents/request#Request

 

 

 

 

 

 

文章来源: fizzz.blog.csdn.net,作者:拿我格子衫来,版权归原作者所有,如需转载,请联系作者。

原文链接:fizzz.blog.csdn.net/article/details/103130030

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。