简体   繁体   English

在 Apps 脚本和 Google 自然语言 API 上循环 function

[英]Loop function on Apps Scripts & Google Natural Language API

I am learning how to code so sorry if this is too basic, but I am getting troubles here:如果这太基本了,我正在学习如何编码,很抱歉,但我在这里遇到了麻烦:

I've been trying to invoke the Google Natural Language API, to give me information about information on 210 rows of my Google Spreadsheet (the whole table has 211 rows).我一直在尝试调用 Google Natural Language API,为我提供有关我的 Google 电子表格 210 行信息的信息(整个表格有 211 行)。 I would like to save the results on 1 Json File.我想将结果保存在 1 个 Json 文件中。

I am trying to run a loop with the code below, but I am getting the Json file only with the information corresponding to the 1st row.我正在尝试使用下面的代码运行一个循环,但我得到的 Json 文件仅包含与第一行对应的信息。 Tried as well to put the "Driveapp.createFile line of code" inside of the loop function, but then I have many Json files, each one with the information corresponding to one row.也尝试将“Driveapp.createFile 代码行”放在循环 function 内,但后来我有很多 Json 文件,每个文件都有对应于一行的信息。 And what I would like is 1 Json file, with the corresponding information of the 210 rows.我想要的是1个Json文件,210行的相应信息。

I would appreciate your help, please.我会很感激你的帮助,拜托。

    function analyzeText() {


  var client = "Spreadsheet_ID";
  var query = SpreadsheetApp.openById(client).getSheetByName("1. Template");
 var result = SpreadsheetApp.openById(client).getSheetByName("Teste - Natural Language API");
 var lrow = query.getLastRow();

  for(var i=2; i<=211;i++)
  {
  var text = query.getRange(i,211).getValue()

  var requestUrl = [
    'https://language.googleapis.com/v1beta2/documents:analyzeEntities?key=',
    'API_KEY_XXXXXXXXXXXXXXXXXXX'
  ].join("");


  var data = {
    "document": {
      "language": "en-us",
      "type": "PLAIN_TEXT",
      "content": text
    },
    "encodingType": "UTF8"
  };

  var options = {
    method : "POST",
    contentType: "application/json",
    payload : JSON.stringify(data)
  };

  var response = UrlFetchApp.fetch(requestUrl, options);

  var data = JSON.parse(response);

  }
 DriveApp.createFile('response3.json', response, MimeType.PLAIN_TEXT);

}

I would suggest you instead of the approach you are taking (using a for loop and the method getValue() , which it's a slow method to call in a loop), consider this one I am giving you with this code:我建议您代替您正在采用的方法(使用for循环和方法getValue() ,这是一种在循环中调用的缓慢方法),考虑一下我给您的代码:

function analyzeText() {
  var clientId = "your-sheet-id";
  var ss =  SpreadsheetApp.openById(clientId);
  var templateSheet = ss.getSheetByName("1. Template");
  // .getRange(row, column, numRows) -> From the first row and col, take the next 4 rows
  // Modify these arguments depending in where you want to start and how many rows you want
  var data = templateSheet.getRange(1, 1, 4).getValues();
  // You will get an array 2D, using join you will able to get an string from
  // all the elements in that array
  var text = data.join();
  var requestUrl = [
    'https://language.googleapis.com/v1beta2/documents:analyzeEntities?key=',
    'API_KEY_XXXXXXXXXXXXXXXXXXX'
  ].join("");
  // Now text will have all your cell values and you only need to do one request
  var data = {
    "document": {
      "language": "en-us",
      "type": "PLAIN_TEXT",
      "content": text
    },
    "encodingType": "UTF8"
  };
  var options = {
    method : "POST",
    contentType: "application/json",
    payload : JSON.stringify(data)
  };
  var response = UrlFetchApp.fetch(requestUrl, options);
  var data = JSON.parse(response);
  DriveApp.createFile('response3.json', response, MimeType.PLAIN_TEXT);
}

In this way, you only need to make one request and it will be faster than running 211 times your loop.这样,您只需要发出一个请求,它会比运行 211 次循环更快。 I would also recommend you to check:我还建议您检查:

  • Apps Script Quotas : Running your code as you have it, it would give you more chances of hitting these quotas. Apps Script Quotas :运行你的代码,它会给你更多达到这些配额的机会。

  • Best Practices : You can check more about the best practices so you can have a better idea about why I was telling you to avoid the getValue() method in a loop. 最佳实践:您可以查看有关最佳实践的更多信息,以便更好地了解我为什么要告诉您在循环中避免使用getValue()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM