Select to view content in your preferred language

Interested in reading the full contents of a CSV file in the media folder into a JavaScript function

78
4
10 hours ago
ChipMorgan65
Occasional Contributor

All,

   I have a Survey 123 Connect form that will allow ecologists to fill out ecological (dominant trees, shrubs, forbs, etc) and abiotic (slope, aspect, drainage, etc) parameters while documenting a location. There are numerous paper/PDF forms to choose from, but my organization chose a modified version of the Virginia Natural Heritage form. It's pretty good.

   What I'd like to do is build the framework to predict the ecosystem based on the answers to the various questions. Assuming that I get expert ecologists from the organization to build the key (obviously, no small feat!), I'd like to load the key data into a CSV file, place the key CSV file in the survey's media folder, read the *entire contents* of the key CSV from a JavaScript function in the survey, and then predict the ecosystem based on the survey inputs.

   There's a lot of work to do here that is outside the scope of this question, but I wanted to provide context for my actual short-term issue, which is: how do I get a JavaScript function to read the full contents of a CSV?

Here's my code fragment:

try {
   // Read the CSV content as a single string using pulldata
   var csvContent = pulldata("@file", "Ecosystem_key.csv");
   // Check if content is undefined or empty

   if (!csvContent) {
     return "Error: CSV content is empty or not found.";
   }
...

 

   The code is getting the error "Error: CSV content is empty or not found". At this point, the script can't distinguish between the file being empty (it's not) and not being found (likely, but unsure why). I'm at the ChatGPT level of (lack of) confidence in JS coding, but it says that I don't need to specify the relative path to the media folder.

   From what I can tell based on a short search, I don't know if I can browse the media directory or not from JavaScript Survey 123. I'm not familiar with JavaScript and not familiar with JS's implementation in Survey 123, so it's unclear what the limitations are.

 Any ideas would be appreciated,

   Chip

 

0 Kudos
4 Replies
ChipMorgan65
Occasional Contributor

I'm thinking that ChatGPT hallucinated the @file parameter, dammit. I also just noticed that the Survey 123 doc says that JavaScript within Survey 123 cannot access local files. I could be doubly screwed here.

0 Kudos
ChipMorgan65
Occasional Contributor

One last point here. The ecologists are frequently working outside of cellular coverage, so pulling the ecological key from an AGO layer wouldn't be reliable.

0 Kudos
DougBrowning
MVP Esteemed Contributor

Is there a reason you are not building a key out and doing a normal lookup using pulldata?

We are also struggling to get ecosites built out but we are working on it spatially which we would then post process or process at the time of analysis. 

0 Kudos
ChipMorgan65
Occasional Contributor

We would build a key, yes, but I'm having a bit of a struggle thinking about how we would use a normal pulldata with the key. I'm very early on in the initial planning on this and I have not coded anything like a hierarchical key in Survey 123. I understand the idea of conditional processing; I've done that. I've thought of pulldata as a one-off "get a single result" lookup. For a hierarchical key, after the first decision point (say forest vs woodland vs savanna vs prairie as a very simple example), there would still be dozens (or more) ecosystems within each answer. What single value would I pull with a normal pulldata?

I very much welcome a response as I think I'm about to learn something pretty useful. Bear with me, please.

0 Kudos