Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google sheet: import a library (CryptoJS)

I'm using the google sheet webscript to manipulate spreadsheet data and I want to use the following function to encrypt certain cells:

var encrypted = CryptoJS.AES.encrypt("message", "Secret key");

There's an option to add libraries to the Google Sheet webscript but I have no idea how to get a library installed. According to the Google documentation you need the project key/script ID in order to use the library, but I have not been able to find this kind of information.

Can someone assist in how to actually import this CryptoJS library to use in the webscript.

enter image description here


enter image description here

like image 461
Michael Tolsma Avatar asked Oct 24 '25 10:10

Michael Tolsma


1 Answers

Issue and workaround:

Unfortunately, in the current stage, it seems that there are no built-in methods for directly achieving the AES encryption in Google Apps Script methods.

So in this case, how about the following workarounds?

Pattern 1:

In this pattern, crypto-js is used.

Usage:

1. Get crypto-js:

Please access to https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js. And copy and paste the script to the script editor.

2. Sample script:

After the copy and paste crypto-js, please copy and paste the following sample script.

function myFunction() {
  var key = "sampleSecretKey";
  var value = "sampleMessage";

  var encryptedMessage = CryptoJS.AES.encrypt(value, key).toString();
  var decryptedMessage = CryptoJS.AES.decrypt(encryptedMessage, key).toString(CryptoJS.enc.Utf8);

  Logger.log(encryptedMessage);
  Logger.log(decryptedMessage);
}
  • When you run the function of myFunction(), the encrypted and decrypted values are returned.

Pattern 2: Updated on April 7, 2022.

In this pattern, "cCryptoGS" which is a Google Apps Script library is used.

Usage:

1. Install Google Apps Script library:

The project key for installing the library is 1IEkpeS8hsMSVLRdCMprij996zG6ek9UvGwcCJao_hlDMlgbWWvJpONrs.

Please install the GAS library using this project key.

2. Sample script:

function myFunction() {
  var key = "sampleSecretKey";
  var value = "sampleMessage";

  var cipher = new cCryptoGS.Cipher(key, 'aes');
  var encryptedMessage = cipher.encrypt(value);
  var decryptedMessage = cipher.decrypt(encryptedMessage);

  Logger.log (encryptedMessage);
  Logger.log (decryptedMessage);
}
  • When you run the function of myFunction, the encrypted and decrypted values are returned.

References:

  • crypto-js at GitHub
  • crypto-js at CDN
  • CryptoJS libraries for Google Apps Script
  • Libraries
  • cCryptoGS
like image 63
Tanaike Avatar answered Oct 27 '25 01:10

Tanaike