Calling set on a appscript Map object doesn't update the Map

173 views
Skip to first unread message

Jonah Thiele

unread,
Jun 14, 2023, 11:00:06 AM6/14/23
to Google Apps Script Community
I need to create a local map variable for this function however when I run my code below the console logs the key and value pair fine, but displays the map as empty {} braces. I want to figure out how to add to a map in google app script.


function generateMap() {
const scriptPrp = PropertiesService.getScriptProperties();
const anglishWordsE = new Map();
//open by the correct Id
var sheet = spreadSheet.getSheetByName("Wordbook");

//select C2 all the way down the column
var unformattedMeanings = sheet.getRange(2,3,sheet.getLastRow() - 1, 1).getValues();
var unformattedWord = sheet.getRange(2,1,sheet.getLastRow() - 1, 1).getValues();

for(var i = 0; i < unformattedMeanings.length; i++)
{
//split based on the dot seperator
const splitMeanings = unformattedMeanings[i].toString().trim().slice(1,-1).split("᛫");
for(var b = 0; b < splitMeanings.length; b++)
{
// get anglish word
const anglishWord = unformattedWord[i].toString();
console.log(anglishWord);
console.log(splitMeanings[b]);
anglishWordsE.set(anglishWord, splitMeanings[b]);
console.log(anglishWordsE);
}
}
//console.log(anglishWordsE);
//scriptPrp.setProperty("anglishWords", JSON.stringify([...anglishWordsE]));


}

my console output:
screenshot.png

What did I miss in creating the map object that is causing the weird errata? I have limited experience with java script so it is possible I missed something elementary.




Tanaike

unread,
Jun 14, 2023, 9:54:32 PM6/14/23
to Google Apps Script Community
I think that when the Map object is seen with console.log(anglishWordsE), unfortunately, the values cannot be directly seen. So, please modify your script as follows.

From
console.log(anglishWordsE);

To
console.log([...anglishWordsE]);



Reply all
Reply to author
Forward
0 new messages