问题
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.
回答1:
How about this answer? Please think of this as just one of several possible 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:
In this pattern, "cCryptoGS" which is a Google Apps Script library is used.
Usage:
1. Install Google Apps Script library:The project key is MSJnPeIon6nzdLewGV60xWqi_d-phDA33
or 1IEkpeS8hsMSVLRdCMprij996zG6ek9UvGwcCJao_hlDMlgbWWvJpONrs
. Both key is the same library.
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
If this was not the direction you want, I apologize.
来源:https://stackoverflow.com/questions/60775116/google-sheet-import-a-library-cryptojs