1

Maybe someone can help me with my Problem.

First some background: I have copied some WhatsApp chats to Google Sheets. I use Latex to generate a book containing the chats. Google Sheet is able to display all Emojis from WhatsApp, Latex, of course, isn't. So I downloaded the Emojis as png files and defined Latex commands to include those emojis as graphic. For Example: Too display the regular smiling emoji I type \grin. I have a list with hundreds of emojis in one row and the corresponding command in the next row. Until now I used search and replace -> replace all in the same sheet for every single type of emoji. But as this takes hours I wondered if there is any way to make this more effective.

Here is a spreadsheet with a small example: Google Sheet

Thanks in advance!

enter image description here

5
  • Where are you searching and replacing against? The same spreadsheet? Commented Jan 8, 2020 at 21:55
  • @IMTheNachoMan yes, I added this information in the post Commented Jan 8, 2020 at 22:00
  • In order to correctly understand your question, can you provide a sample Spreadsheet? Of course, please remove your personal information. Commented Jan 9, 2020 at 0:44
  • @Tanaike i made a short conversation with a chat, how it is supposed to look afterwards. you can find it here: docs.google.com/spreadsheets/d/… Commented Jan 9, 2020 at 5:51
  • Thank you for replying and sharing the sample Spreadsheet. From your shared Spreadsheet, I proposed a sample script as answer. Could you please confirm it? If I misunderstood your question and that was not the result you want, I apologize. Commented Jan 9, 2020 at 6:29

1 Answer 1

2
  • You want to replace the Emojis to the defined Latex commands.
    • For example, you want to replace as follows.
    • From 03.01.19, 00:29 - me: Hi 😊 to 03.01.19, 00:29 - me: Hi \nettnett.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Flow:

The flow of this sample script is as follows. This sample script uses your shared Spreadsheet.

  1. Retrieve data from the sheet of Codes for Smileys.
  2. Create the request body for the findReplace request of batchUpdate method of Sheets API.
  3. Run the method of batchUpdate.

Sample script:

This script used Sheets API. So, before you run the script, please enable Sheets API at Advanced Google services.

function myFunction() {
  var dataSheet = "Codes for Smileys";
  var sourceSheet = "unedited Chats with Smileys";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var data = ss.getSheetByName(dataSheet).getDataRange().getValues();
  data.shift();
  var sheetId = ss.getSheetByName(sourceSheet).getSheetId();
  var requests = data.map(function(row) {return {findReplace: {sheetId: sheetId, find: row[0], replacement: row[1]}}});
  Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
}
  • This sample script uses your shared Spreadsheet. So in this case, the data sheet is Codes for Smileys. And the source sheet for converting is unedited Chats with Smileys.

Note:

  • If you change the sheet name, also please modify above script. Please be careful this.
  • When you run the script for the first time, the authorization screen is opened. So please authorize the scopes for using the script.

References:

Added:

  • You want to put the converted values to the sheet of Chat with Latex Code.

The sample script for achieving above is as follows.

Sample script:

function myFunction2() {
  var dataSheet = "Codes for Smileys";
  var sourceSheet = "unedited Chats with Smileys";
  var destinationSheet = "Chats with LaTeX Codes";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(dataSheet);
  var data = ss.getSheetByName(dataSheet).getDataRange().getValues();
  data.shift();
  var srcSheet = ss.getSheetByName(sourceSheet);
  var tempSheet = srcSheet.copyTo(ss);
  var sheetId = tempSheet.getSheetId();
  var requests = data.map(function(row) {return {findReplace: {sheetId: sheetId, find: row[0], replacement: row[1]}}});
  Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
  var tempValues = tempSheet.getDataRange().getValues();
  var destSheet = ss.getSheetByName(destinationSheet);
  destSheet.getRange(destSheet.getLastRow() + 1, 1, tempValues.length, tempValues[0].length).setValues(tempValues);
  ss.deleteSheet(tempSheet);
}
  • In this sample script, the following flow is run.
    1. Copy the source sheet unedited Chats with Smileys as a temporal sheet.
    2. Create request body for the batchUpdate method to the temporal sheet.
    3. Run the batchUpdate.
    4. Copy the converted values from the temporal sheet to the destination sheet Chats with LaTeX Codes.
      • In this case, the converted values are put to the last row of the sheet.
    5. Delete the temporal sheet.
Sign up to request clarification or add additional context in comments.

6 Comments

This is really great! Thank you so much!! One last question: If the transformed chat with Latex commands shall appear in the sub spreadsheet "Chat with Latex Code", how could I achieve this?
Have you also considered the usage of the Apps Script TextFinder class? I believe that with it and its replaceAllWith() method this same functionality could be accomplished. Cheers
@simande Thank you for replying. I'm glad your issue was resolved. About your additional question, if you want to put the converted values to the sheet of Chat with Latex Code, in this case, at first, it copies the sheet of unedited Chats with Smileys as a temporal sheet and converts the values, then, it copies them to the sheet of Chat with Latex Code. By this flow, your additional question can be achieved. If I misunderstood your question, I apologize.
@carlesgg97 Thank you for your comment. Of course, TextFinder can be also used as you say. In this case, it is required to create the TextFinder for each data that OP wants to convert. So I proposed to use the bachUpdate method of Sheets API which can be used by one API call and the request body can be easily created. If I misunderstood your comment, I apologize.
@simande I added one more sample script for achieving your additional question. Could you please confirm it? If I misunderstood your goal, I apologize.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.