Question

J'utilise des scripts Google pour un peu de magie Google Spreadsheets et l'analyse syntaxique automatique de la date qui me rend fou! La feuille que je travaille avec est jonché de toutes sortes de formats de date et je traiterait bien plutôt avec eux comme des chaînes, que d'avoir Google de les convertir en objets de date avec chaque .getValues ??() appel. Après de longues recherches, je ne peux toujours pas trouver un moyen d'arrêter / désactiver Google de l'analyse automatiquement ces chaînes. Est-ce que quelqu'un sait comment?

Était-ce utile?

La solution

Yes it is possible, but you must make a change before calling getValues, and use getDisplayValues():

  // do not use getValues(), which will convert dates
  var string_data = my_sheet.getRange(...).getDisplayValues();

If you only want to use the display value in the limited case of, say, dates, you can copy both values and displays, then test whether your value is a date, and if it is, use the corresponding display instead:

  // use getDisplayValues() for dates, getValues() otherwise
  var string_data = my_sheet.getRange(...).getDisplayValues();
  var value_data =  my_sheet.getRange(...).getValues();

  value_data.forEach(function(row_vals, i) {
    row.forEach(function(cell_val, j) {
      if(is_date(cell_val)) // see e.g. https://stackoverflow.com/a/17110249/3160967
        do_something_with(string_data(i, j));
      else
        do_something_with(cell_val);
    });
  });

Lastly, the above needs modification if you want to use it as a sheet function, because function calls automatically apply getValues() before the function code is executed. To get around this, you can use the techniques described at https://webapps.stackexchange.com/a/30784

Autres conseils

It's indeed not possible, but a feasible workaround is to create an auxiliary sheet and insert a formula to concatenate all values on the source sheet with an empty string. Then getValues() on this auxiliary sheet, which may be hidden or removed by the script after used.

The formula can be as simple as setting this on A1 of the auxiliar sheet

=ArrayFormula(Sheet1!A:T&"")

A caveat with this workaround is that if the user set a custom formatting for his dates, they will not be kept. But if that's important you can get them by using getNumberFormats()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top