Google Apps Script: How to fix “Truncating output”

2019-08-20 05:06发布

问题:

I have a Google Apps Script function which I want to use to do this: extract some data (a date) from a raw text in a spreadsheet cell. When I use a javascript IDE, the code works fine. But when I try it on GAS, it doesn't work.

This is the code:

function findDate (text){
text1 = text.split(".Date");
Logger.log("text1", text1);
//console.log("text1= ", text1);
date = 'no date informed';

for (var i=0; i<text1.length; i++) {
     text2 = text1[i].split(" ");
     Logger.log("text2", text2);
//     console.log("text2= ", text2);
  //   console.log("text2[1]= ", text2[1]);
     if (text2[1] === 'common:'){
         date = text2[2];
       Logger.log("text2[2]", text2[2]);
//         console.log("text2[2]= ", text2[2]);

     }
  }
  return date;
}

The string which is in the spreadsheet cell is not exactly this, it's writen in another language, but it's similar to this (broken text, but without the space between lines):

special term: 19 years, 6 months and 0 days.

commom term: 8 years, 8 months and 0 days.

Date special: 23/11/1998

Date common: 09/11/2012

When I get the logs, I got the message Logging output too large. Truncating output. and this text:

[text1, [[special term: 19 years, 6 months and 0 days. commom term: 8 years, 8 months and 0 days. Date special: 23/11/1998 Date commom: 09/11/2012]]]

THANKS in advance for any help!

回答1:

The Logger is just a "tool" to log (!) results in a code execution, it has a limited size of what it can show in the popup window and that's why it truncates the results you see.

That doesn't mean your code is not working (although I didn't check that).

If you remove the first occurrence in your code (that shows the initial array data and takes a lot of space) you will probably get the entire result.


EDIT following your comment and trying to reproduce :

using the data as above and the code as below

function myFunction() {
  var text = SpreadsheetApp.getActive().getRange("A1").getValue();
  Logger.log('text = '+text);
  Logger.log('function result = '+findDate(text));

}


function findDate (text){
  var text1 = text.split("\n"+"Date");
  Logger.log("text1 = "+ text1);
  var date = 'no date informed';

  for (var i=0; i<text1.length; i++) {
    var text2 = text1[i].split(" ");
    Logger.log("text2 = "+text2);
    if (text2[1] === 'common:'){
      date = text2[2];
      Logger.log("text2[2] = "+ text2[2]);
    }
  }
  return date;
}

I get the following result :