# Encode text or substitute

**URL:** <https://community.glideapps.com/t/encode-text-or-substitute/27352>\
**Category:** Ask for Help\
**Created:** [May 28, 2021, 2:06pm UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352 "2021-05-28T14:06:56Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [May 29, 2021, 6:12am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/21 "2021-05-29T06:12:24Z")

</div>

Already got to this point before starting this post…  
As I said my problem is that the text is dynamic and every user can enter a different text so a template can’t work for me as I don’t know in advance where the commas will be as well as I can’t do for the spaces and as I’m using fetch for the images and not uploading to cloudinary my image must be encoded as well 🤦🏻‍♂️.

On the sheet side I just encoded the image and used substitute function to change any comma in the text dynamically with what you just sent and then encoding the whole text to create a url that is compliant

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [May 29, 2021, 6:16am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/22 "2021-05-29T06:16:54Z")

</div>

It doesn’t really matter that the text is dynamic - just run it through a template similar to the one I posted just now ⬆ and the commas will be encoded wherever they appear.

---

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [May 29, 2021, 7:07am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/23 "2021-05-29T07:07:34Z")

</div>

This looks promising 🤩. Will test asap.  
Now just need to figure how to encode the image itself as I use fetch

---

<div class="post-metadata">

**Author:** ![Krivo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/krivo/32/5363_2.png) [@Krivo](https://community.glideapps.com/u/Krivo)\
**Post date:** [May 29, 2021, 7:37am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/24 "2021-05-29T07:37:29Z")

</div>

@yinon_raviv from curiosity - are you fetching glide images or 3rd party images?

---

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [May 29, 2021, 8:01am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/25 "2021-05-29T08:01:59Z")

</div>

Glide images, why are you asking?

---

<div class="post-metadata">

**Author:** ![Krivo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/krivo/32/5363_2.png) [@Krivo](https://community.glideapps.com/u/Krivo)\
**Post date:** [May 29, 2021, 8:15am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/26 "2021-05-29T08:15:07Z")

</div>

@yinon_raviv I did a sample app recently where I auto-uploaded the images to Cloudinary - and thereafter deleted the images from Glide keeping the storage low in Glide. Kind of prefer that to fetch as fetch images are put in the root of the cloudinary account.

> [@Photo-location - and images AUTO-UPLOADED to Cloudinary](https://community.glideapps.com/t/photo-location-and-images-auto-uploaded-to-cloudinary/26936):
>
> Main features Upload a photo to Glide and mark it with a location Auto-upload the photos to Cloudinary Resizing and other maniputations of the photo uploaded to Cloudinary Remove the photo from Glide View all the photos uploaded - and directly sourced from Cloudinary account Photo and location saved to both Glide Table and Google Sheets Idea Photo and location is inputted to the glide table “Input…” and data are copied to “Registrations” Photo and location is then cleared from …

There are some differences between fetch and auto-upload - maybe you already know those.

> **[What are the differences between Auto-Upload and Fetch?](https://support.cloudinary.com/hc/en-us/articles/211869909-What-are-the-differences-between-Auto-Upload-and-Fetch-)**
>
>  
> 
>     
> 
> 
>  
> Auto-Upload
> Fetch
> 
> 
> Retrieved Images
> Cloudinary provides full management functionalities (like "upload" type). All features available.
> Managed by the user, Cloudinary provides transform...

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [May 29, 2021, 8:19am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/27 "2021-05-29T08:19:19Z")

</div>

> [@yinon\_raviv](#):
>
> Now just need to figure how to encode the image itself as I use fetch

As I’m sure you’re aware, you need to Base64 encode images when using fetch.  
If you can find a way to do that in Glide I’ll be super-impressed, but it’s trivial with Apps Script.

Here’s a short snippet that I use for Base64 encoding:

```auto
  var file = DriveApp.getFileById(file_id);
  var blob = file.getBlob();
  var file_string = Utilities.base64Encode(blob.getBytes());

```

You just need to give it the GDrive FileID, which is easy enough to get.

---

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [May 29, 2021, 9:18am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/28 "2021-05-29T09:18:11Z")

</div>

This is a nice idea. Will get deeper to understand the process.  
Thank you for sharing

---

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [May 29, 2021, 9:21am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/29 "2021-05-29T09:21:19Z")

</div>

Thanks, yes, I’m aware and actually have a different script for the base64 which is just if you use overlay image, in case if one image, regular encoding suffice.

I did not find a way to do this is glide and 8 think this is to complex to convert to glide.

I did manage to regular encode an image for the fetch for 1 image only with replacing in glide template all the slashes (/) with a %2F which works perfectly.

---

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [June 30, 2021, 4:30am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/30 "2021-06-30T04:30:45Z")

</div>

Hi,

It seems my base64 encoding script has the tendency to break and stop working occasionally…  
Does your script stable?  
If so, will be happy to implement it instead of mine with a little guidance from you

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [June 30, 2021, 4:59am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/31 "2021-06-30T04:59:20Z")

</div>

What sort of errors does yours throw when it breaks?  
Might not be too difficult to debug and make it a bit more stable.  
Can you show me the code?

---

<div class="post-metadata">

**Author:** ![yinon\_raviv](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/yinon_raviv/32/1627_2.png) [@yinon\_raviv](https://community.glideapps.com/u/yinon_raviv)\
**Post date:** [June 30, 2021, 5:56am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/32 "2021-06-30T05:56:30Z")

</div>

It’s actually a script that creats a function to be used within the sheet.  
from time to time it needs to be deleted and paste again to restart/

here;s the code:

```auto
**
 * Base64 Encode Input
 * @param {any | Array<any[]>} input - Input cell, or range of cells
 * @param {boolean} [OPT_webSafe=true] - If should use websafe variant of base64
 * @param {boolean} [OPT_plainText=false] - If should treat input as plaintext instead of UTF-8
 */
function base64Encode(input, OPT_webSafe, OPT_plainText) {
  if (!input) return input;
  const charSet = OPT_plainText ? Utilities.Charset.US_ASCII : Utilities.Charset.UTF_8;
  const useWebSafe = OPT_webSafe !== false;
  const encoder = useWebSafe ? Utilities.base64EncodeWebSafe : Utilities.base64Encode;
  if (Array.isArray(input)) {
    return input.map(t => base64Encode(t, OPT_webSafe, OPT_plainText));
  }

  return encoder(input, charSet);
}

/**
 * Base64 Decode Input
 * @param {any | Array<any[]>} input - Input cell, or range of cells
 * @param {boolean} [OPT_webSafe=true] - If should use websafe variant of base64
 * @param {boolean} [OPT_plainText=false] - If should treat input as plaintext instead of UTF-8
 */
function base64Decode(input, OPT_webSafe, OPT_plainText) {
  if (!input) return input;
  const charSet = OPT_plainText ? Utilities.Charset.US_ASCII : Utilities.Charset.UTF_8;
  const useWebSafe = OPT_webSafe !== false;
  const decoder = useWebSafe ? Utilities.base64DecodeWebSafe : Utilities.base64Decode;
  if (Array.isArray(input)) {
    return input.map(t => base64Decode(t, OPT_webSafe, OPT_plainText));
  }

  return Utilities.newBlob(decoder(input, charSet)).getDataAsString();
}

```

would love to have your thoughts and even jump on quick call.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [June 30, 2021, 6:16am UTC](https://community.glideapps.com/t/encode-text-or-substitute/27352/33 "2021-06-30T06:16:41Z")

</div>

When you say it needs to be deleted and pasted again, I assume you’re referring to the values in the spreadsheet, yes?

This is a fairly common problem with custom functions, and for that reason I’m not a big fan.

I’d recommend using a trigger instead. Without seeing what your sheet looks like, I’d probably do something like this:

- Add another column to the sheet to hold a timestamp - “Encoded At”
- Each time the trigger runs, check every row that holds the text to be encoded
- If the corresponding timestamp column is empty, then encode the text in that row and write the current time to the timestamp column

The thing about this approach is if it fails once for any row (due to a transient sheets error), then the timestamp won’t be written and it should then pick it up on the next run.

I can be available for a call, if you’d like to talk it through, just PM me.

[Previous page](https://community.glideapps.com/t/encode-text-or-substitute/27352.md?page=1)
