1

When I try to run the following code in google apps script

  var numArray = [31, -117, 8, 8, -102, -124, 75, 88, 2, 0, 106, 117, 108, 121, 46, 116, 120, 116, 0, 1, 4, 0, -5, -1, 106, 117, 108, 121, -13, -113, 116, -57, 4, 0, 0, 0];
  var typedArray     = new Uint8Array(numArray);

...I get:

ReferenceError: "Uint8Array" is not defined.

At the same time

  var numArray = [31, -117, 8, 8, -102, -124, 75, 88, 2, 0, 106, 117, 108, 121, 46, 116, 120, 116, 0, 1, 4, 0, -5, -1, 106, 117, 108, 121, -13, -113, 116, -57, 4, 0, 0, 0];
  var typedArray     = new Array(numArray);

...works just fine. Is there a clever workaround way to implement a Uint8Array in google apps script?

7
  • What's the goal of using Uint8 bit arrays? Is that just to save space on Googles server? How big do you expect the array to be? Commented Dec 11, 2016 at 9:40
  • 3
    GAS runs on Rhino, and probably not the latest version; typed arrays are not supported. Commented Dec 11, 2016 at 15:32
  • 1
    @Xepoch, could I ask you to share your "byte flopping" code? Sounds like you were able to successfully imitate the functionality of a Uint8Array. Commented Dec 12, 2016 at 20:14
  • 1
    @July.Tech, dsp.stackexchange.com/a/36220/25336 Commented Dec 12, 2016 at 22:55
  • 1
    @Xepoch, this bit from the code snippet that you linked to effectively answers my question: vegaArray[i]<0?vegaArray[i]+256:vegaArray[i];. Thank you! Commented Dec 13, 2016 at 4:29

1 Answer 1

2

OK, so thanks to the comment from @Xepoch, here is the answer to my original question.

The equivalent to

  var numArray = [31, -117, 8, 8, -102, -124, 75, 88, 2, 0, 106, 117, 108, 121, 46, 116, 120, 116, 0, 1, 4, 0, -5, -1, 106, 117, 108, 121, -13, -113, 116, -57, 4, 0, 0, 0];
  var typedArray     = new Uint8Array(numArray);

is (in the absence of Uint8Array):

  var numArray = [31, -117, 8, 8, -102, -124, 75, 88, 2, 0, 106, 117, 108, 121, 46, 116, 120, 116, 0, 1, 4, 0, -5, -1, 106, 117, 108, 121, -13, -113, 116, -57, 4, 0, 0, 0];
  var typedArray = [];
  for(var i=0;i<numArray .length;i++) {
     typedArray.push(numArray [i]<0?numArray [i]+256:numArray [i]);
  }
Sign up to request clarification or add additional context in comments.

Comments

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.