@mcpher/gas-fakes 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +561 -97
- package/package.json +9 -4
- package/src/services/advsheets/fakeadvsheets.js +899 -221
- package/src/services/advsheets/fakeadvsheetsdevelopermetadata.js +65 -0
- package/src/services/advsheets/fakeadvsheetsspreadsheets.js +28 -16
- package/src/services/commonclasses/fakeborder.js +2 -2
- package/src/services/commonclasses/fakecolorbuilder.js +7 -1
- package/src/services/commonclasses/fakerichtextvalue.js +131 -0
- package/src/services/commonclasses/fakerun.js +38 -0
- package/src/services/commonclasses/faketextrotation.js +10 -21
- package/src/services/commonclasses/faketextstyle.js +31 -15
- package/src/services/commonclasses/faketextstylebuilder.js +26 -24
- package/src/services/commonclasses/fakewrapstrategy.js +15 -5
- package/src/services/spreadsheetapp/datavalidationcriteriamapping.js +0 -1
- package/src/services/spreadsheetapp/fakebanding.js +350 -0
- package/src/services/spreadsheetapp/fakebigquerydatasourcespec.js +52 -0
- package/src/services/spreadsheetapp/fakebigquerydatasourcespecbuilder.js +62 -0
- package/src/services/spreadsheetapp/fakedatasource.js +172 -0
- package/src/services/spreadsheetapp/fakedatasourcecolumn.js +162 -0
- package/src/services/spreadsheetapp/fakedatasourcecolumnreference.js +25 -0
- package/src/services/spreadsheetapp/fakedatasourceparameter.js +40 -0
- package/src/services/spreadsheetapp/fakedatasourcespec.js +78 -0
- package/src/services/spreadsheetapp/fakedatasourcespecbuilder.js +102 -0
- package/src/services/spreadsheetapp/fakedatatable.js +69 -0
- package/src/services/spreadsheetapp/fakedevelopermetadata.js +112 -0
- package/src/services/spreadsheetapp/fakedevelopermetadatafinder.js +185 -0
- package/src/services/spreadsheetapp/fakedevelopermetadatalocation.js +54 -0
- package/src/services/spreadsheetapp/fakefilter.js +169 -0
- package/src/services/spreadsheetapp/fakefiltercriteria.js +100 -0
- package/src/services/spreadsheetapp/fakefiltercriteriabuilder.js +224 -0
- package/src/services/spreadsheetapp/fakepivotfilter.js +131 -0
- package/src/services/spreadsheetapp/fakepivotgroup.js +185 -0
- package/src/services/spreadsheetapp/fakepivottable.js +260 -0
- package/src/services/spreadsheetapp/fakepivotvalue.js +170 -0
- package/src/services/spreadsheetapp/fakerun.js +38 -0
- package/src/services/spreadsheetapp/fakesheet.js +333 -367
- package/src/services/spreadsheetapp/fakesheetrange.js +1618 -414
- package/src/services/spreadsheetapp/fakesheetrangelist.js +10 -2
- package/src/services/spreadsheetapp/fakesortspec.js +69 -0
- package/src/services/spreadsheetapp/fakespreadsheet.js +94 -15
- package/src/services/spreadsheetapp/fakespreadsheetapp.js +49 -10
- package/src/services/spreadsheetapp/sheetrangehelpers.js +200 -145
- package/src/services/spreadsheetapp/sheetrangemakers.js +848 -0
- package/src/support/auth.js +24 -7
- package/src/support/helpers.js +2 -2
- package/src/support/patchedmakesynchronous.js +80 -0
- package/src/support/sheetutils.js +120 -5
- package/src/support/sxapi.js +1 -0
- package/src/support/sxauth.js +4 -7
- package/src/support/sxdrive.js +2 -0
- package/src/support/syncit.js +26 -17
- package/src/support/utils.js +119 -22
- package/tsynk/t.mjs +10 -0
- package/tsynk/w.mjs +13 -0
- package/src/services/typedefs.js +0 -302
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
I use clasp/vscode to develop Google Apps Script (GAS) applications, but when using GAS native services, there's way too much back and fowards to the GAS IDE going while testing. I set myself the ambition of implementing fake version of the GAS runtime environment on Node so I could at least do some testing locally.
|
|
4
4
|
|
|
5
|
-
This is
|
|
5
|
+
This is a proof of concept so I've implemented a subset of number of services and methods, but the tricky parts are all in place so all that's left is a load of busy work (to which I heartily invite any interested collaborators).
|
|
6
6
|
|
|
7
7
|
## progress
|
|
8
8
|
|
|
@@ -20,23 +20,106 @@ The idea is that you can run GAS services (so far implemented) locally on Node,
|
|
|
20
20
|
|
|
21
21
|
Just as on Apps Script, everything is executed synchronously so you don't need to bother with handling Promises/async/await. Note that the intended audience is Apps Script developers who want to run the same code and access the same services in both Node and Apps Script.
|
|
22
22
|
|
|
23
|
-
If you don't plan on using Apps Script at all, the Node Workspace APIs (which I use in the background for all these services in any case) will be more efficient
|
|
23
|
+
If you don't plan on using Apps Script at all, the Node Workspace APIs (which I use in the background for all these services in any case) will be more efficient when operating in their normal asynchronous mode. On the other hand, if you only casually want to access workspace resources from Node, and can't be bothered digging into how the Node Workspace APIs work, you could still use this as part of your node project. Apps Script services are much simpler than the full API. Apps Script advanced services are also available via gas-fakes if you want a hybrid solution.
|
|
24
24
|
|
|
25
25
|
### Cloud project
|
|
26
26
|
|
|
27
|
-
You don't have access to the GAS maintained cloud project, so you'll need to create a GCP project to use locally. In order to duplicate the OAuth management handled by GAS, we'll use Application Default
|
|
27
|
+
You don't have access to the GAS maintained cloud project, so you'll need to create a GCP project to use locally. In order to duplicate the OAuth management handled by GAS, we'll use Application Default Credentials.
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
There are some scripts in this repo in the ./shells folder to set up and test these, but first you'll need to add some stuff to the .env file. First copy the .env.template to .env. More info on this is availble in the setup-env.MD file.
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
This is the template - at the very least you need to add the gcp project id you'll be using for testing, plus the id of some file you have access to - this'll be used to check that you have set up ADC properly.
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
If you want to run the test suite, there are some other things required in the .env file as below - you may need to request access to some of the resources mentions, or you could replace with your own. If you just want to get going using gas-fakes on Node, you can just ignore all that.
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
# for testing authentication
|
|
37
|
+
GCP_PROJECT_ID="add your gcp project id here"
|
|
38
|
+
DRIVE_TEST_FILE_ID="add the id of some test file you have access to here"
|
|
39
|
+
|
|
40
|
+
# we'll use the default config for application default credentials
|
|
41
|
+
AC=default
|
|
42
|
+
# these are the scopes set by default - take some of these out if you want to minimize access
|
|
43
|
+
DEFAULT_SCOPES="https://www.googleapis.com/auth/userinfo.email,openid,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/sqlservice.login"
|
|
44
|
+
EXTRA_SCOPES=",https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/spreadsheets"
|
|
45
|
+
|
|
46
|
+
# everything below here is required for the test suite if you are using it otherwise you can ignore everything below here
|
|
47
|
+
# you can ask me for access to the shared files if you are running the test suite, or you can create your own versions
|
|
48
|
+
|
|
49
|
+
# set your email address to test various apis
|
|
50
|
+
SHARED_FILE_OWNER="bruce@mcpher.com"
|
|
51
|
+
EMAIL="bruce@mcpher.com"
|
|
52
|
+
TIMEZONE="Europe/London"
|
|
53
|
+
TEST_LOCALE="en"
|
|
54
|
+
OWNER_NAME="Bruce Mcpherson"
|
|
55
|
+
|
|
56
|
+
# these are files that are used to test some drive features
|
|
57
|
+
TEST_BORDERS_ID="1hRGdrYHEPixXTuQLeL3Z0qGRZVs_8ojMIm6D4KrCh1o"
|
|
58
|
+
TEST_AIRPORTS_ID="1h9IGIShgVBVUrUjjawk5MaCEQte_7t32XeEP1Z5jXKQ"
|
|
59
|
+
TEST_AIRPORTS_NAME="airport list"
|
|
60
|
+
TEST_FOLDER_NAME="math"
|
|
61
|
+
TEST_FOLDER_FILES=3
|
|
62
|
+
TEST_FOLDER_ID="1Zww9oCTFR7zYcUYXxd70yQr3sw6VdLG-"
|
|
63
|
+
TEXT_FILE_NAME="fake.txt"
|
|
64
|
+
TEXT_FILE_ID="1142Vn7W-pGl5nWLpUSkpOB82JDiz9R6p"
|
|
65
|
+
TEXT_FILE_TYPE="text/plain"
|
|
66
|
+
TEXT_FILE_CONTENT="foo is not bar"
|
|
67
|
+
BLOB_NAME="foo.txt"
|
|
68
|
+
BLOB_TYPE="text/plain"
|
|
69
|
+
TEST_SHEET_ID="1DlKpVVYCrCPNfRbGsz6N_K3oPTgdC9gQIKi0aNb42uI"
|
|
70
|
+
TEST_SHEET_NAME="sharedlibraries"
|
|
71
|
+
PUBLIC_SHARE_FILE_ID="1OFJk38kW9TRrEf-B9F1gTZk2uLV-ZSpR"
|
|
72
|
+
SHARED_FILE_ID="1uz4cxEDxtQzu0cBb1B4h6fsjgWy7hNFf"
|
|
73
|
+
PDF_ID="1v5kJ5SOY2nu3DI1LKwALb3seaBpF3kWu"
|
|
74
|
+
|
|
75
|
+
# these are parameters for testing
|
|
76
|
+
MIN_ROOT_PDFS=20
|
|
77
|
+
MIN_PDFS=400
|
|
78
|
+
MIN_FOLDERS_ROOT=110
|
|
79
|
+
SKIP_SINGLE_PARENT=1
|
|
80
|
+
|
|
81
|
+
ZIP_TYPE="application/zip"
|
|
82
|
+
KIND_DRIVE="drive#file"
|
|
83
|
+
|
|
84
|
+
RANDOM_IMAGE="https://picsum.photos/200"
|
|
85
|
+
API_URL="http://suggestqueries.google.com/complete/search?client=chrome&hl=en&q=trump"
|
|
86
|
+
API_TYPE="text/javascript"
|
|
87
|
+
CLEAN=1
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### OAuth
|
|
91
|
+
|
|
92
|
+
There's 2 pieces to this solution.
|
|
93
|
+
|
|
94
|
+
#### Application default credentials (ADC)
|
|
95
|
+
|
|
96
|
+
In order to avoid a bunch of Node specific code and credentials, yet still handle OAuth, I figured that we could simply rely on ADC. This is a problem I already wrote about here [Application Default Credentials with Google Cloud and Workspace APIs](https://ramblings.mcpher.com/application-default-credentials-with-google-cloud-and-workspace-apis/)
|
|
97
|
+
|
|
98
|
+
This section in your env file controls which scopes you plan to use.
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
we'll use the default config for application default credentials
|
|
102
|
+
AC=default
|
|
103
|
+
# these are the scopes set by default - take some of these out if you want to minimize access
|
|
104
|
+
DEFAULT_SCOPES="https://www.googleapis.com/auth/userinfo.email,openid,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/sqlservice.login"
|
|
105
|
+
EXTRA_SCOPES=",https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/spreadsheets"
|
|
106
|
+
|
|
107
|
+
.....etc
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Manifest file
|
|
111
|
+
|
|
112
|
+
**gas-fakes** reads the manifest file to see which scopes you need in your project, uses the Google Auth library to attempt to authorizes them and has `ScriptApp.getOauthToken()` return a sufficiently specced token, just as the GAS environment does. Just make sure you have an `appsscript.json` in the same folder as your main script.
|
|
113
|
+
|
|
114
|
+
Now you can execute this and it will set up your ADC to be able to run any services that require the scopes you add.
|
|
115
|
+
|
|
116
|
+
##### note
|
|
34
117
|
|
|
35
|
-
|
|
118
|
+
Although you may be tempted to add `https://www.googleapis.com/auth/script.external_request`, it's not necessary for the ADC and in fact will generate an error. You will of course need it in your Apps script manifest.
|
|
36
119
|
|
|
37
120
|
### Settings
|
|
38
121
|
|
|
39
|
-
gasfakes.json holds various location and behavior parameters to inform about your Node environment. It's not required on GAS as you can't change anything over there. If you don't have one, it'll create one for you and use some sensible defaults. Here's an example of one with the defaults. It should be in the same folder as your main script.
|
|
122
|
+
Optionally, gasfakes.json holds various location and behavior parameters to inform about your Node environment. It's not required on GAS as you can't change anything over there. If you don't have one or need one, it'll create one for you and use some sensible defaults. Here's an example of one with the defaults. It should be in the same folder as your main script.
|
|
40
123
|
|
|
41
124
|
```
|
|
42
125
|
{
|
|
@@ -60,19 +143,6 @@ gasfakes.json holds various location and behavior parameters to inform about you
|
|
|
60
143
|
|
|
61
144
|
More on all this later.
|
|
62
145
|
|
|
63
|
-
### Pushing to GAS
|
|
64
|
-
|
|
65
|
-
The script togas.sh will move your files to GAS - just set the `SOURCE` and `TARGET` folders in the script. Make sure you have an `appsscript.json` manifest in the `SOURCE` folder, as **gas-fakes** reads that to handle OAuth on Node.
|
|
66
|
-
|
|
67
|
-
You can write your project to run on Node and call GAS services, and it will also run on the GAS environment with no code changes, except on the Node side you have this one import
|
|
68
|
-
|
|
69
|
-
```sh
|
|
70
|
-
// all the fake services are here
|
|
71
|
-
import '@mcpher/gas-fakes/main.js'
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
togas.sh will remove imports and exports on the way to apps script, which doesnt support them.
|
|
75
|
-
|
|
76
146
|
## Approach
|
|
77
147
|
|
|
78
148
|
Google have not made details about the GAS run time public (as far as I know). What we do know is that it used to run on a Java based JavaScript emulator [Rhino](https://ramblings.mcpher.com/gassnippets2/what-javascript-engine-is-apps-script-running-on/) but a few years ago moved to a V8 runtime. Beyond that, we don't know anything much other than it runs on Google Servers somewhere.
|
|
@@ -92,58 +162,7 @@ Although Apps Script supports async/await/promise syntax, it operates in blockin
|
|
|
92
162
|
|
|
93
163
|
Since asynchonicity is fundamental to Node, there's no real simple way to convert async to sync. However, there is such a thing as a [child-process](https://nodejs.org/api/child_process.html#child-process) which you can start up to run things, and it features an [execSync](https://nodejs.org/api/child_process.html#child_processexecsynccommand-options) method which delays the return from the child process until the promise queue is all settled. So the simplest solution is to run an async method in a child process, wait till it's done, and return the results synchronously. I found that [Sindre Sorhus](https://github.com/sindresorhus) uses this approach with [make-synchronous](https://github.com/sindresorhus/make-synchronous), so I'm using that.
|
|
94
164
|
|
|
95
|
-
Runnng up a child process in Node is pretty expensive and slow
|
|
96
|
-
|
|
97
|
-
### OAuth
|
|
98
|
-
|
|
99
|
-
There's 2 pieces to this solution.
|
|
100
|
-
|
|
101
|
-
#### Application default credentials (ADC)
|
|
102
|
-
|
|
103
|
-
In order to avoid a bunch of Node specific code and credentials, yet still handle OAuth, I figured that we could simply rely on ADC. This is a problem I already wrote about here [Application Default Credentials with Google Cloud and Workspace APIs](https://ramblings.mcpher.com/application-default-credentials-with-google-cloud-and-workspace-apis/)
|
|
104
|
-
|
|
105
|
-
To set this up, set your GCP project ID and the extra scopes you'll need in `shells/setaccount.sh`. In this example I'm retaining the usual ADC scopes, and adding an extra scope to be able to access Drive.
|
|
106
|
-
|
|
107
|
-
```sh
|
|
108
|
-
# project ID
|
|
109
|
-
P=YOUR_GCP_PROJECT_ID
|
|
110
|
-
|
|
111
|
-
# config to activate - multiple configs can each be named
|
|
112
|
-
# here we're working on the default project configuration
|
|
113
|
-
AC=default
|
|
114
|
-
|
|
115
|
-
# these are the ones it sets by default - take some of these out if you want to minimize access
|
|
116
|
-
DEFAULT_SCOPES="https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/drive,openid,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/sqlservice.login"
|
|
117
|
-
|
|
118
|
-
# these are the ones we want to add (note comma at beginning)
|
|
119
|
-
EXTRA_SCOPES=",https://www.googleapis.com/auth/drive"
|
|
120
|
-
|
|
121
|
-
.....etc
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
Now you can execute this and it will set up your ADC to be able to run any services that require the scopes you add.
|
|
125
|
-
|
|
126
|
-
##### note
|
|
127
|
-
|
|
128
|
-
Although you may be tempted to add `https://www.googleapis.com/auth/script.external_request`, it's not necessary for the ADC and in fact will generate an error. You will of course need it in your Apps script manifest.
|
|
129
|
-
|
|
130
|
-
##### testing ADC
|
|
131
|
-
|
|
132
|
-
`shells/testtoken.sh` can test that you can generate a token with sufficient scope. In this example, I'm checking that I can access a file I own. Change the id to one of your own.
|
|
133
|
-
|
|
134
|
-
```js
|
|
135
|
-
# check tokens have scopes required for DRIVE access
|
|
136
|
-
# set below to a fileid on drive you have access to
|
|
137
|
-
FILE_ID=SOME_FILE_ID
|
|
138
|
-
|
|
139
|
-
....etc
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
I recommend you do this to make sure Auth it's all good before you start coding up your app.
|
|
143
|
-
|
|
144
|
-
#### Manifest file
|
|
145
|
-
|
|
146
|
-
**gas-fakes** reads the manifest file to see which scopes you need in your project, uses the Google Auth library to attempt to authorizes them and has `ScriptApp.getOauthToken()` return a sufficiently specced token, just as the GAS environment does. Just make sure you have an `appsscript.json` in the same folder as your main script.
|
|
165
|
+
Runnng up a child process in Node is pretty expensive and slow, so I'll be looking for ways to speed that up when I get to it.
|
|
147
166
|
|
|
148
167
|
### Global intialization
|
|
149
168
|
|
|
@@ -151,13 +170,13 @@ This was a little problematic to sequence, but I wanted to make sure that any GA
|
|
|
151
170
|
|
|
152
171
|
Only a subset of methods are currently available for some of them - the rest are work in progress. My approach is to start with a little bit of each service to prove feasibility and provide a base to build on.
|
|
153
172
|
|
|
154
|
-
v1.0.
|
|
173
|
+
v1.0.10
|
|
155
174
|
|
|
156
175
|
- `DriveApp` - 50%
|
|
157
176
|
- `ScriptApp` - almost all
|
|
158
177
|
- `UrlFetchApp` - 80%
|
|
159
178
|
- `Utilities` - almost all
|
|
160
|
-
- `Sheets` -
|
|
179
|
+
- `Sheets` - 50%
|
|
161
180
|
- `SpreadsheetApp` - 60%
|
|
162
181
|
- `CacheService` - 80%
|
|
163
182
|
- `PropertiesService` - 80%
|
|
@@ -165,10 +184,11 @@ v1.0.8
|
|
|
165
184
|
- `Blob` - all
|
|
166
185
|
- `User` - all
|
|
167
186
|
- `Drive (Advanced Service)` - 40%
|
|
187
|
+
- `Sheets (Advanced Service)` - 40%
|
|
168
188
|
|
|
169
189
|
### Testing coverage
|
|
170
190
|
|
|
171
|
-
Tests for all methods are added as we go to the cumulative unit tests and run on both Apps Script and Node. The goal is to try to get the behavior as exactly equivalent as possible.
|
|
191
|
+
Tests for all methods are added as we go to the cumulative unit tests and run on both Apps Script and Node. The goal is to try to get the behavior as exactly equivalent as possible. There are currently almost 4000 active tests.
|
|
172
192
|
|
|
173
193
|
#### Proxies and globalThis
|
|
174
194
|
|
|
@@ -192,7 +212,7 @@ All 3 are supported for both properties and cache.
|
|
|
192
212
|
|
|
193
213
|
##### scriptId
|
|
194
214
|
|
|
195
|
-
The local version may have no knowledge of the Apps ScriptId. If you are using clasp, it's picked up from the .clasp.json file. However if you are not using clasp, or want to use something else, you can set the scriptId in `gasfakes.json`, otherwise it'll create a fake id use that. All property and cache stores use the scriptId to partition data.
|
|
215
|
+
The local version may have no knowledge of the Apps ScriptId. If you are using clasp, it's picked up from the .clasp.json file. However if you are not using clasp, or want to use something else, you can set the scriptId in `gasfakes.json`, otherwise it'll create a fake id use that - but that means that each store will be different each time you run it, so the best approach is to add a value unique to the group of scripts that want to share the same stores in gas-fakes.json. All property and cache stores use the scriptId to partition data.
|
|
196
216
|
|
|
197
217
|
##### userId
|
|
198
218
|
|
|
@@ -206,17 +226,220 @@ The documentId is only meaningful if you are working on a container bound scrip.
|
|
|
206
226
|
|
|
207
227
|
As you will have noticed, there are various local support files for props/caching etc. Be careful that these do not get committed to a public repo if you are adding sensitive values to your stores. Note that the real user Id is not used when creating files, but rather an encrypted version of it. This avoids real user ids being revealed in your file system.
|
|
208
228
|
|
|
229
|
+
## Debugging
|
|
230
|
+
|
|
231
|
+
For conversion of async to sync, I'm spawing a subprocess using [make-synchronous](https://github.com/sindresorhus/make-synchronous). Out of the box it inherits the Node Options from the main process, so that means it'll try to run each subprocess in debug mode also. Bringing up and down the debugger each time takes forever, so I've temporaily modified my local version of make-synchronous to drop the debug inheritance.
|
|
232
|
+
|
|
233
|
+
If this makes it to the repo we can start to use it from there - see issue https://github.com/sindresorhus/make-synchronous/issues/14
|
|
234
|
+
|
|
209
235
|
## Noticed differences
|
|
210
236
|
|
|
211
|
-
|
|
237
|
+
In the main, these will be slight differences in error message text, which I'll normalize over time, or where Apps Script has a fundamental obstacle. Please report any differences in behavior you find in the repo issues.
|
|
212
238
|
|
|
213
239
|
### Tradeoffs
|
|
214
240
|
|
|
215
|
-
I've come across various Apps Script bugs/issues as I work through this which I've reported to the GAS team, and added workarounds in the gas fakes code - not sure at this point whether to duplicate the buggy behavior or simulate what would seem to be the correct one.
|
|
241
|
+
I've come across various Apps Script bugs/issues as I work through this which I've reported to the GAS team, and added workarounds in the gas fakes code - not sure at this point whether to duplicate the buggy behavior or simulate what would seem to be the correct one. This is not a complete list, so any things you come across please use the issues in the repo to report.
|
|
216
242
|
|
|
217
243
|
## Oddities
|
|
218
244
|
|
|
219
|
-
Just a few things I've come across when digging into the differences between what the sheets API and Apps Script do. Whether or not you use gas fakes, some of this stuff might be useful if you are using the Sheets API directly, or indeed the Sheets Advanced service.
|
|
245
|
+
Just a few things I've come across when digging into the differences between what the sheets API and Apps Script do. Whether or not you use gas fakes, some of this stuff might be useful if you are using the Sheets API directly, or indeed the Sheets Advanced service. I'll just make a growing list of stuff I've found, in no particular order.
|
|
246
|
+
|
|
247
|
+
### Note to collaborators
|
|
248
|
+
|
|
249
|
+
Gemini code assist can be a very helpful for the busy work, but there a huge number of inconsistencies between what it believes to be the documentation and the actual real world, so if Gemini starts flailing take over early.
|
|
250
|
+
|
|
251
|
+
You eventually have to dig into the docs yourself to track down why something Gemini advised isn't working.
|
|
252
|
+
|
|
253
|
+
Gemini can also write test cases, but it tends to miss adding edge cases, so don't rely on Gemini completely for that, and always get the tests working Apps Script side first to ensure it is behaving as expected (often it's not) - after all that's what we're trying to emulate.
|
|
254
|
+
|
|
255
|
+
### Colors
|
|
256
|
+
|
|
257
|
+
#### Named Colors
|
|
258
|
+
|
|
259
|
+
In addition to CSS hex notation (e.g., `#ff0000`), Apps Script methods like `Range.setBackground()` and `Range.setFontColor()` also accept standard CSS color names. The fake environment supports all 147 standard names, which are treated case-insensitively.
|
|
260
|
+
|
|
261
|
+
| Name | Hex Value |
|
|
262
|
+
| ---------------------- | --------- |
|
|
263
|
+
| `aliceblue` | `#f0f8ff` |
|
|
264
|
+
| `antiquewhite` | `#faebd7` |
|
|
265
|
+
| `aqua` | `#00ffff` |
|
|
266
|
+
| `aquamarine` | `#7fffd4` |
|
|
267
|
+
| `azure` | `#f0ffff` |
|
|
268
|
+
| `beige` | `#f5f5dc` |
|
|
269
|
+
| `bisque` | `#ffe4c4` |
|
|
270
|
+
| `black` | `#000000` |
|
|
271
|
+
| `blanchedalmond` | `#ffebcd` |
|
|
272
|
+
| `blue` | `#0000ff` |
|
|
273
|
+
| `blueviolet` | `#8a2be2` |
|
|
274
|
+
| `brown` | `#a52a2a` |
|
|
275
|
+
| `burlywood` | `#deb887` |
|
|
276
|
+
| `cadetblue` | `#5f9ea0` |
|
|
277
|
+
| `chartreuse` | `#7fff00` |
|
|
278
|
+
| `chocolate` | `#d2691e` |
|
|
279
|
+
| `coral` | `#ff7f50` |
|
|
280
|
+
| `cornflowerblue` | `#6495ed` |
|
|
281
|
+
| `cornsilk` | `#fff8dc` |
|
|
282
|
+
| `crimson` | `#dc143c` |
|
|
283
|
+
| `cyan` | `#00ffff` |
|
|
284
|
+
| `darkblue` | `#00008b` |
|
|
285
|
+
| `darkcyan` | `#008b8b` |
|
|
286
|
+
| `darkgoldenrod` | `#b8860b` |
|
|
287
|
+
| `darkgray` | `#a9a9a9` |
|
|
288
|
+
| `darkgreen` | `#006400` |
|
|
289
|
+
| `darkgrey` | `#a9a9a9` |
|
|
290
|
+
| `darkkhaki` | `#bdb76b` |
|
|
291
|
+
| `darkmagenta` | `#8b008b` |
|
|
292
|
+
| `darkolivegreen` | `#556b2f` |
|
|
293
|
+
| `darkorange` | `#ff8c00` |
|
|
294
|
+
| `darkorchid` | `#9932cc` |
|
|
295
|
+
| `darkred` | `#8b0000` |
|
|
296
|
+
| `darksalmon` | `#e9967a` |
|
|
297
|
+
| `darkseagreen` | `#8fbc8f` |
|
|
298
|
+
| `darkslateblue` | `#483d8b` |
|
|
299
|
+
| `darkslategray` | `#2f4f4f` |
|
|
300
|
+
| `darkslategrey` | `#2f4f4f` |
|
|
301
|
+
| `darkturquoise` | `#00ced1` |
|
|
302
|
+
| `darkviolet` | `#9400d3` |
|
|
303
|
+
| `deeppink` | `#ff1493` |
|
|
304
|
+
| `deepskyblue` | `#00bfff` |
|
|
305
|
+
| `dimgray` | `#696969` |
|
|
306
|
+
| `dimgrey` | `#696969` |
|
|
307
|
+
| `dodgerblue` | `#1e90ff` |
|
|
308
|
+
| `firebrick` | `#b22222` |
|
|
309
|
+
| `floralwhite` | `#fffaf0` |
|
|
310
|
+
| `forestgreen` | `#228b22` |
|
|
311
|
+
| `fuchsia` | `#ff00ff` |
|
|
312
|
+
| `gainsboro` | `#dcdcdc` |
|
|
313
|
+
| `ghostwhite` | `#f8f8ff` |
|
|
314
|
+
| `gold` | `#ffd700` |
|
|
315
|
+
| `goldenrod` | `#daa520` |
|
|
316
|
+
| `gray` | `#808080` |
|
|
317
|
+
| `green` | `#008000` |
|
|
318
|
+
| `greenyellow` | `#adff2f` |
|
|
319
|
+
| `grey` | `#808080` |
|
|
320
|
+
| `honeydew` | `#f0fff0` |
|
|
321
|
+
| `hotpink` | `#ff69b4` |
|
|
322
|
+
| `indianred` | `#cd5c5c` |
|
|
323
|
+
| `indigo` | `#4b0082` |
|
|
324
|
+
| `ivory` | `#fffff0` |
|
|
325
|
+
| `khaki` | `#f0e68c` |
|
|
326
|
+
| `lavender` | `#e6e6fa` |
|
|
327
|
+
| `lavenderblush` | `#fff0f5` |
|
|
328
|
+
| `lawngreen` | `#7cfc00` |
|
|
329
|
+
| `lemonchiffon` | `#fffacd` |
|
|
330
|
+
| `lightblue` | `#add8e6` |
|
|
331
|
+
| `lightcoral` | `#f08080` |
|
|
332
|
+
| `lightcyan` | `#e0ffff` |
|
|
333
|
+
| `lightgoldenrodyellow` | `#fafad2` |
|
|
334
|
+
| `lightgray` | `#d3d3d3` |
|
|
335
|
+
| `lightgreen` | `#90ee90` |
|
|
336
|
+
| `lightgrey` | `#d3d3d3` |
|
|
337
|
+
| `lightpink` | `#ffb6c1` |
|
|
338
|
+
| `lightsalmon` | `#ffa07a` |
|
|
339
|
+
| `lightseagreen` | `#20b2aa` |
|
|
340
|
+
| `lightskyblue` | `#87cefa` |
|
|
341
|
+
| `lightslategray` | `#778899` |
|
|
342
|
+
| `lightslategrey` | `#778899` |
|
|
343
|
+
| `lightsteelblue` | `#b0c4de` |
|
|
344
|
+
| `lightyellow` | `#ffffe0` |
|
|
345
|
+
| `lime` | `#00ff00` |
|
|
346
|
+
| `limegreen` | `#32cd32` |
|
|
347
|
+
| `linen` | `#faf0e6` |
|
|
348
|
+
| `magenta` | `#ff00ff` |
|
|
349
|
+
| `maroon` | `#800000` |
|
|
350
|
+
| `mediumaquamarine` | `#66cdaa` |
|
|
351
|
+
| `mediumblue` | `#0000cd` |
|
|
352
|
+
| `mediumorchid` | `#ba55d3` |
|
|
353
|
+
| `mediumpurple` | `#9370db` |
|
|
354
|
+
| `mediumseagreen` | `#3cb371` |
|
|
355
|
+
| `mediumslateblue` | `#7b68ee` |
|
|
356
|
+
| `mediumspringgreen` | `#00fa9a` |
|
|
357
|
+
| `mediumturquoise` | `#48d1cc` |
|
|
358
|
+
| `mediumvioletred` | `#c71585` |
|
|
359
|
+
| `midnightblue` | `#191970` |
|
|
360
|
+
| `mintcream` | `#f5fffa` |
|
|
361
|
+
| `mistyrose` | `#ffe4e1` |
|
|
362
|
+
| `moccasin` | `#ffe4b5` |
|
|
363
|
+
| `navajowhite` | `#ffdead` |
|
|
364
|
+
| `navy` | `#000080` |
|
|
365
|
+
| `oldlace` | `#fdf5e6` |
|
|
366
|
+
| `olive` | `#808000` |
|
|
367
|
+
| `olivedrab` | `#6b8e23` |
|
|
368
|
+
| `orange` | `#ffa500` |
|
|
369
|
+
| `orangered` | `#ff4500` |
|
|
370
|
+
| `orchid` | `#da70d6` |
|
|
371
|
+
| `palegoldenrod` | `#eee8aa` |
|
|
372
|
+
| `palegreen` | `#98fb98` |
|
|
373
|
+
| `paleturquoise` | `#afeeee` |
|
|
374
|
+
| `palevioletred` | `#db7093` |
|
|
375
|
+
| `papayawhip` | `#ffefd5` |
|
|
376
|
+
| `peachpuff` | `#ffdab9` |
|
|
377
|
+
| `peru` | `#cd853f` |
|
|
378
|
+
| `pink` | `#ffc0cb` |
|
|
379
|
+
| `plum` | `#dda0dd` |
|
|
380
|
+
| `powderblue` | `#b0e0e6` |
|
|
381
|
+
| `purple` | `#800080` |
|
|
382
|
+
| `red` | `#ff0000` |
|
|
383
|
+
| `rosybrown` | `#bc8f8f` |
|
|
384
|
+
| `royalblue` | `#4169e1` |
|
|
385
|
+
| `saddlebrown` | `#8b4513` |
|
|
386
|
+
| `salmon` | `#fa8072` |
|
|
387
|
+
| `sandybrown` | `#f4a460` |
|
|
388
|
+
| `seagreen` | `#2e8b57` |
|
|
389
|
+
| `seashell` | `#fff5ee` |
|
|
390
|
+
| `sienna` | `#a0522d` |
|
|
391
|
+
| `silver` | `#c0c0c0` |
|
|
392
|
+
| `skyblue` | `#87ceeb` |
|
|
393
|
+
| `slateblue` | `#6a5acd` |
|
|
394
|
+
| `slategray` | `#708090` |
|
|
395
|
+
| `slategrey` | `#708090` |
|
|
396
|
+
| `snow` | `#fffafa` |
|
|
397
|
+
| `springgreen` | `#00ff7f` |
|
|
398
|
+
| `steelblue` | `#4682b4` |
|
|
399
|
+
| `tan` | `#d2b48c` |
|
|
400
|
+
| `teal` | `#008080` |
|
|
401
|
+
| `thistle` | `#d8bfd8` |
|
|
402
|
+
| `tomato` | `#ff6347` |
|
|
403
|
+
| `turquoise` | `#40e0d0` |
|
|
404
|
+
| `violet` | `#ee82ee` |
|
|
405
|
+
| `wheat` | `#f5deb3` |
|
|
406
|
+
| `white` | `#ffffff` |
|
|
407
|
+
| `whitesmoke` | `#f5f5f5` |
|
|
408
|
+
| `yellow` | `#ffff00` |
|
|
409
|
+
| `yellowgreen` | `#9acd32` |
|
|
410
|
+
|
|
411
|
+
##### rebeccapurple
|
|
412
|
+
|
|
413
|
+
This is an interesting html color name that apps script does not support, so I've omitted that from the color name support. To learn more about this color name see - https://medium.com/@valgaze/the-hidden-purple-memorial-in-your-web-browser-7d84813bb416
|
|
414
|
+
|
|
415
|
+
#### Banding Themes
|
|
416
|
+
|
|
417
|
+
The colors used for banding themes can change over time with UI updates from Google. The `gas-fakes` library maintains a map of the current colors to match the live environment. The `Banding Theme Colors Verification` test in `testsheetssets.js` is used to validate these.
|
|
418
|
+
|
|
419
|
+
| Theme | Header | First Band | Second Band | Footer |
|
|
420
|
+
| ------------- | --------- | ---------- | ----------- | --------- |
|
|
421
|
+
| `LIGHT_GREY` | `#bdbdbd` | `#ffffff` | `#f3f3f3` | `#dedede` |
|
|
422
|
+
| `CYAN` | `#4dd0e1` | `#ffffff` | `#e0f7fa` | `#a2e8f1` |
|
|
423
|
+
| `GREEN` | `#63d297` | `#ffffff` | `#e7f9ef` | `#afe9ca` |
|
|
424
|
+
| `YELLOW` | `#f7cb4d` | `#ffffff` | `#fef8e3` | `#fce8b2` |
|
|
425
|
+
| `ORANGE` | `#f46524` | `#ffffff` | `#ffe6dd` | `#ffccbc` |
|
|
426
|
+
| `BLUE` | `#5b95f9` | `#ffffff` | `#e8f0fe` | `#acc9fe` |
|
|
427
|
+
| `TEAL` | `#26a69a` | `#ffffff` | `#ddf2f0` | `#8cd3cd` |
|
|
428
|
+
| `GREY` | `#78909c` | `#ffffff` | `#ebeff1` | `#bbc8ce` |
|
|
429
|
+
| `BROWN` | `#cca677` | `#ffffff` | `#f8f2eb` | `#e6d3ba` |
|
|
430
|
+
| `LIGHT_GREEN` | `#8bc34a` | `#ffffff` | `#eef7e3` | `#c4e2a0` |
|
|
431
|
+
| `INDIGO` | `#8989eb` | `#ffffff` | `#e8e7fc` | `#c4c3f7` |
|
|
432
|
+
| `PINK` | `#e91d63` | `#ffffff` | `#fddce8` | `#f68ab0` |
|
|
433
|
+
|
|
434
|
+
### Fake classes
|
|
435
|
+
|
|
436
|
+
Most Apps script classes will map to a separate fake class file - sometimes more than one. Many of the methods in large classes are generated from various specification files, but the more complex ones and the ones with weird behavior are directly written as methods in the class.
|
|
437
|
+
|
|
438
|
+
i've tried to avoid adding properties and methods that don't exist in the emulated class, but sometimes it's necessary to have private methods and properties. Since Apps Script doesn't support private properties, I've decided to simply identify these with a leading pair of underscores, eg this.\_\_myProperty.
|
|
439
|
+
|
|
440
|
+
Although not strictly necessary to avoid real private propertues, since these Fake classes will exist only on Node, I wanted to keep code comaptible with Apps Script.
|
|
441
|
+
|
|
442
|
+
Most classes have a new method -- eg `newFakeClass(args)`. It's best to use this rather than `new FakeClass(args)`, since they each wrap the instance created in a proxy that detects attempts to access non existent properties, or indeed to set any properties other than private ones. Very handy when debugging.
|
|
220
443
|
|
|
221
444
|
### Formats and styles
|
|
222
445
|
|
|
@@ -229,7 +452,7 @@ This means that sometimes, for example, a font might be red in the UI, but Apps
|
|
|
229
452
|
|
|
230
453
|
### Values
|
|
231
454
|
|
|
232
|
-
Just as with Formats, the actual value rendered might be different than the value stored. For example the number 1 might be displayed as '1' but returned as 1, and visa versa depending on the effective format for its range. I'm not entrely sure at this point the exact rules that getValues() applies, but this is what I've implemented - which appears to get the results most similar to App Script.
|
|
455
|
+
Just as with Formats, the actual value rendered might be different than the value stored. For example the number 1 might be displayed as '1' but returned as 1, and visa versa depending on the effective format for its range. I'm not entrely sure at this point the exact rules that getValues() applies, but this is what I've implemented - which appears to get the results most similar to App Script.
|
|
233
456
|
|
|
234
457
|
Here is how I've implemented getting and setting values.
|
|
235
458
|
|
|
@@ -245,24 +468,12 @@ There's quite a few oddities in Data Validation, which turned out to be the most
|
|
|
245
468
|
|
|
246
469
|
A few of the criteria types differ between the Sheets API and Apps Script - for example TEXT_IS_VALID_EMAIL on GAS is equivalent to TEXT_IS_EMAIL on the API, and VALUE_IN_LIST is equivalent to ONE_OF_LIST and a few others. I tried using Gemini to help tabulate the differences but there were too many errors for that to be a trustworthy source.
|
|
247
470
|
|
|
248
|
-
Here's an example Gemini reponse during multiple back and forward conversations.
|
|
249
|
-
|
|
250
|
-
```
|
|
251
|
-
You are absolutely, completely, and unequivocally correct! My apologies for this ongoing and unacceptable level of inaccuracy. You are demonstrating remarkable patience and a keen eye for detail.
|
|
252
|
-
```
|
|
253
|
-
|
|
254
471
|
The file 'fakedatavalidationcriteria.js' has a list of the final mappings between the 2.
|
|
255
472
|
|
|
256
473
|
#### Relative dates
|
|
257
474
|
|
|
258
475
|
Both the sheets API and GAS can return either relative dates or actual dates. In Sheets, you'll see a relativeDate property versus a userEnteredValue, whereas in GAS you get a different code to the one expected - so in other words a criteria type you expect to return DATE_EQUAL, might instead return DATE_EQUAL_TO_RELATIVE.
|
|
259
476
|
|
|
260
|
-
As usual, Gemini is no help in this.
|
|
261
|
-
|
|
262
|
-
```
|
|
263
|
-
You are absolutely correct, and I apologize profusely for the significant inaccuracies in my previous list of SpreadsheetApp.DataValidationCriteria properties. My information was clearly outdated and unreliable. Thank you for providing the complete and correct list.
|
|
264
|
-
```
|
|
265
|
-
|
|
266
477
|
##### Setting a relative date
|
|
267
478
|
|
|
268
479
|
There are no methods in Apps Script to actually set relative dates in Data Validation - for example you'd expect a method such as requireDateEqualToRelative to exist - but it doesn't - to set you'd need to use the advanced sheets service or the withCriteria method. However this does not work - see this Apps Script issue - https://issuetracker.google.com/issues/418495831
|
|
@@ -305,7 +516,7 @@ These 3 relatives exist as keys of SpreadsheetApp.DataValidationCriteria, but no
|
|
|
305
516
|
- DATE_BEFORE_RELATIVE
|
|
306
517
|
- DATE_EQUAL_TO_RELATIVE
|
|
307
518
|
|
|
308
|
-
I'll implement these 3 realtives in gasFakes, but treat the others as invalid. However, you cannot set these as the sheets API doesnt support seting of relative dates with Data Validation and neither does GAS - which doesnt throw an error. I believe it should I'm going to throw an error if you try.
|
|
519
|
+
I'll implement these 3 realtives in gasFakes, but treat the others as invalid. However, you cannot set these as the sheets API doesnt support seting of relative dates with Data Validation and neither does GAS - which doesnt throw an error. I believe it should, so I'm going to throw an error if you try.
|
|
309
520
|
|
|
310
521
|
#### datavalidation with formulas
|
|
311
522
|
|
|
@@ -336,10 +547,175 @@ I'm leaving these same behaviors in place, and you would need to use the same wo
|
|
|
336
547
|
|
|
337
548
|
Since only relative versions of single dates are implemented in GAS, there's no need to handle mixed relative and real dates. As an aside, there's no validation in the UI, so you can enter any nonsense in the from and to values.
|
|
338
549
|
|
|
550
|
+
#### sheets notes
|
|
551
|
+
|
|
552
|
+
Normally, range.setNote ("takes a string"). However it does allow a numeric argument as well, which it converts to a string. However a normal toString() - for example 25.toString() would give "25". Apps script however returns "25.0" if we use getNotes() on a range whose notes has been set with setNotes() but "25" if the note was set with setNote().
|
|
553
|
+
|
|
554
|
+
There's an issue reported here - https://issuetracker.google.com/issues/429373214 - for now I'm returing "25.0" in all cases till we see what the actual resolution of this issue should be
|
|
555
|
+
|
|
339
556
|
#### Locale of dates
|
|
340
557
|
|
|
341
558
|
CriteriaValues are stored as a string, exactly as typed by the user. This means that if the API is operating in a different locale to the sheet, date formats will be different and wrong (for example - 20/2/23 in UK is 2/20/23 in US). This is a problem you would anyway face in Apps Script so I don't plan to handle this right now.
|
|
342
559
|
|
|
560
|
+
## Various hints when using the advanced sheets service
|
|
561
|
+
|
|
562
|
+
I've tried to exactly imitate the behavior of the Sheets advanced service (even though it's often inconvenient and inconsistent), so these following comments apply to both Sheets and FakeSheets. If you are usng the Advanced service, here's a few hints Ive come across that might be helpful.
|
|
563
|
+
|
|
564
|
+
### Advanced sheets updating cells
|
|
565
|
+
|
|
566
|
+
The advanced sheets service provides a huge list of builders such as Sheets.newCellData(). This is supposed to simplify building requests using the Sheets service, rather than building the requests from scratch your self. I sometimes find them more long winded that just making the objects, and I notice that there are no checks on the values that you set using them, so there's not any validation to proft from.
|
|
567
|
+
|
|
568
|
+
In any case, I've implemented them all (note that there are some GAS bugs on some of these) - https://issuetracker.google.com/issues/423737982)
|
|
569
|
+
|
|
570
|
+
I mainly use them when emulating Apps Script SpreadsheetApp services too as a double check that they are working as intended, but sometimes I build the requests up from scratch if it makes the automation simpler.
|
|
571
|
+
|
|
572
|
+
If you want to see how these are all generated, see the constructor in services/advsheets/fakeadvsheets.
|
|
573
|
+
|
|
574
|
+
#### Handling multiple response variations and formats.
|
|
575
|
+
|
|
576
|
+
If you retrieve a cell format that has been set in the UI (or in Apps Script), you often get a less full response than one that has been set using the API. If you are using the Advanced Sheets Service, and you ask for "numberFormat" for example, you may get just the pattern (0.###) or you may get the full cellformat data { type: "NUMBER", pattern: "0.###""}. You'll have to be ready to handle either type of response depending on how (and perhaps even when) the value was originally created. This could apply to any fetches of format values.
|
|
577
|
+
|
|
578
|
+
Something like this should do the trick.
|
|
579
|
+
|
|
580
|
+
```js
|
|
581
|
+
const extractPattern = (response) => {
|
|
582
|
+
// a plain pattern entered by UI, apps script or lax api call
|
|
583
|
+
if (is.string(response)) return response;
|
|
584
|
+
// should be { type: "TYPE", pattern: "xxx"}
|
|
585
|
+
if (!is.object(response) || !Reflect.has(response, "pattern")) return null;
|
|
586
|
+
return response.pattern;
|
|
587
|
+
};
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
To emulate the regular SpreadsheetApp behavior, `fakeRange.getNumberFormat()` will strip out any extra stuff and just return the pattern. `fakeRange.setNumberFormat("0.###")` will always set the complete cellformat object { type: "NUMBER", pattern: "0.###"}
|
|
591
|
+
|
|
592
|
+
##### Numberformat default pattern
|
|
593
|
+
|
|
594
|
+
Normally we can use a null value to reset a format to the default UI value. However, number format will fail messily with a null argument. The correct way is `setNumberFormat('general')` even though `getNumberFormat()` returns '0.###############" or similar. If using Advanced Sheets, you still need to use the 'pattern' approach - { pattern: "general", type: "NUMBER" }
|
|
595
|
+
|
|
596
|
+
#### Text direction
|
|
597
|
+
|
|
598
|
+
Unlike other similar functions, `setTextDirection(TextDirection)` takes an enum argument and `getTextDirection()` returns an enum too. `setTextDirection(null)` will reset to default behavior, but a subsequent `getTextDirection()` will return null, rather than a default value. This allows the Sheets UI to make an in context decision based on language locale.
|
|
599
|
+
|
|
600
|
+
#### Horizontal alignment
|
|
601
|
+
|
|
602
|
+
The documented acceptable values to `range.setHorizontalAlignment()` are left, center, normal, null. However right is also valid so I'm supporting that too. `range.getHorizontalAlignment()` returns left,center,right,general,general-left. Although the alignment behavior for 'general' and 'general-left' in the UI appears identical, `range.setHorizontalAlignment(null)` returns 'general', whereas `range.setHorizontalAlignment('normal')` returns 'general-left'. There doesn't appear to be a way to force a 'general-left' return via the Sheets API or advanced service.
|
|
603
|
+
|
|
604
|
+
As with most of these format setting methods, Apps Script will silently ignore invalid arguments. I've generally throw an error if an invalid value argument is sent so, by design, `range.setHorizontalAlignment('foo') will throw an error on FakeGas, but not on Apps Script.
|
|
605
|
+
|
|
606
|
+
#### Wrap and Wrap strategy
|
|
607
|
+
|
|
608
|
+
Initially a cell will return OVERFLOW for `getWrapStrategy` and true for `getWrap`. This is wrong as OVERFLOW should be paired with false. Once you set wrapStrategy explicitly to OVERFLOW, it returns the correct value of false.
|
|
609
|
+
|
|
610
|
+
The Apps Script issue for that is here https://issuetracker.google.com/issues/427134600
|
|
611
|
+
|
|
612
|
+
#### range.copyValuesToRange
|
|
613
|
+
|
|
614
|
+
The documentaton for this method says - "Copy the content of the range to the given location. If the destination is larger or smaller than the source range then the source is repeated or truncated accordingly."
|
|
615
|
+
|
|
616
|
+
This implies that a smaller destination range that the source should only paste a truncated version of the source range. In fact it pastes it all - see issue https://issuetracker.google.com/issues/427192537
|
|
617
|
+
|
|
618
|
+
So in summary the current behavior of this function in Apps Script doesn't match the documentation in these ways:
|
|
619
|
+
|
|
620
|
+
- If the target range is smaller than the source range, it does not truncate, but always copies the entire range even if it violates the dimensions of the target range.
|
|
621
|
+
- If the target range is larger than the source range, it only duplicates enough times where it can fit the entire source data into what's remaining in the target range.
|
|
622
|
+
|
|
623
|
+
#### range.copyTo
|
|
624
|
+
|
|
625
|
+
The variant of copyToRange suffers from the same problems as .copyValuesToRange. But there are others too. I've decided to implement them cleanly in the hope that the issues in Apps Script will one day be fixed - see issue https://issuetracker.google.com/issues/427192537
|
|
626
|
+
|
|
627
|
+
1. I also note that range.copyTo() has the same behavior
|
|
628
|
+
|
|
629
|
+
2. the documentation for copyTo says "A destination range to copy to; only the top-left cell position is relevant." - This is not true - since duplication or truncation will happen depending on the size of the output range, just as with range.copyValuesToRange and range.copyFormatToRange().
|
|
630
|
+
|
|
631
|
+
3. There is no way to pass "transposed" when using the range.copyTo(destination, options) variant.
|
|
632
|
+
|
|
633
|
+
4. There is no checking on the enum passed as the 2nd argument unless the optional transpose argumment is provided
|
|
634
|
+
|
|
635
|
+
```
|
|
636
|
+
range.copyTo (destination) // valid
|
|
637
|
+
range.copyTo (destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES) // valid
|
|
638
|
+
range.copyTo (destination, "FOO", true) // correctly reports FOO as invalid type
|
|
639
|
+
range.copyTo (destination, "FOO") // ignores FOO and runs without reporting error
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
5. There is no conflicting option error thrown for
|
|
643
|
+
|
|
644
|
+
```
|
|
645
|
+
range.copyTo(destination, { contentsOnly: true, formatOnly: true }) // should throw error for conflicting options
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
6. Does not check for invalid options
|
|
649
|
+
|
|
650
|
+
```
|
|
651
|
+
range.copyTo(targetRange, { foo: true }) // should throw an error for invalid option
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
7. Paste values can also reset paste formats (doesnt happen with advanced sheets)
|
|
655
|
+
|
|
656
|
+
```
|
|
657
|
+
range.copyTo (destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES) /// this can also trash formats previously set with PASTE_FORMAT
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
#### checking of invalid arguments
|
|
661
|
+
|
|
662
|
+
In many cases, Apps Script doesn't check the validity of invalid arguments - for example (sse this issue https://issuetracker.google.com/issues/428869869), it happily accepts invalid colors.
|
|
663
|
+
|
|
664
|
+
```
|
|
665
|
+
range.setBackground("foo")
|
|
666
|
+
range.setBackground("#gggggg")
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
Generally I type check most arguments, so may throw an error when Apps Script doesn't - but I also raise as an issue with the Apps Script team in the hope they'll fix it one day. As a result you may find some t.threw() tests are skipped when running in the real Apps Script environment.
|
|
670
|
+
|
|
671
|
+
#### TextRotation
|
|
672
|
+
|
|
673
|
+
Apps Script returns a `TextRotation` object to `range.getTextRotation()`, which has both an 'isVertical()' and `getDegrees()` method. There is an overload for the `setTextRotation(degrees)` function - `setTextRotation(TextRotation)` which theoretically allows you to set a vertical or and angle. https://developers.google.com/apps-script/reference/spreadsheet/range#settextrotationrotation
|
|
674
|
+
|
|
675
|
+
However, unlike most objects like this, there is not a `SpreadsheetApp.newTextRotation()`, and the object returned by `getTextRotation()` is readonly with no set variants. Trying to pass a plain JavaScript object with the assumed properties results in this error.
|
|
676
|
+
|
|
677
|
+
```
|
|
678
|
+
Exception: The parameters ((class)) don't match the method signature for SpreadsheetApp.Range.setTextRotation.
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
So the conclusion is that the overload for `setTextRotation(TextRotation)` does not work, so I won't be implementing this until the issue is resolved. `setTextRotation(degrees)` has been implemented of course.
|
|
682
|
+
|
|
683
|
+
[See this issue for more information ](https://issuetracker.google.com/issues/425390984)
|
|
684
|
+
|
|
685
|
+
Here's Gemini's verdict on textRotation
|
|
686
|
+
|
|
687
|
+
"You are absolutely right, and I sincerely apologize once again for the continuous string of incorrect information regarding SpreadsheetApp's TextRotation capabilities. This specific part of the Apps Script API is surprisingly complex and poorly documented/intuitive."
|
|
688
|
+
|
|
689
|
+
There's also a bug in the advanced sheet service - it doesn't return an angle in its response, even though it is set in the UI and even though Range.getTextRotation() correctly returns the angle. See https://issuetracker.google.com/issues/425390984.
|
|
690
|
+
|
|
691
|
+
Since I'm using the API I can't detect the angle until that issue is fixed, so an angle set by the UI will always be seen as 0.
|
|
692
|
+
|
|
693
|
+
#### Dates and sheets advanced service
|
|
694
|
+
|
|
695
|
+
Dates can be stored in 'Excel dateserial' format in the API. This is a float showing how many days have passed since the Excel epoch which was Dec 30th, 1899. Here's a function to convert JS dates to that, which may be helpful if you are using the sheets advanced service, rather than the SpreadsheetApp service.
|
|
696
|
+
|
|
697
|
+
```js
|
|
698
|
+
const dateToSerial = (date) => {
|
|
699
|
+
if (!is.date(date)) {
|
|
700
|
+
throw new Error(`dateToSerial is expecting a date but got ${is(date)}`);
|
|
701
|
+
}
|
|
702
|
+
// these are held in a serial number like in Excel, rather than JavaScript epoch
|
|
703
|
+
// so the epoch is actually Dec 30 1899 rather than Jan 1 1970
|
|
704
|
+
const epochCorrection = 2209161600000;
|
|
705
|
+
const msPerDay = 24 * 60 * 60 * 1000;
|
|
706
|
+
const adjustedMs = date.getTime() + epochCorrection;
|
|
707
|
+
return adjustedMs / msPerDay;
|
|
708
|
+
};
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
To enter this, you submit do this to create the value for your updateCells request body.
|
|
712
|
+
|
|
713
|
+
```js
|
|
714
|
+
const value = Sheets.newExtendedValue().setNumberValue(dateToSerial(value));
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
Note that this simply enters the numeric value of the dateSerial, without mentioning that it actually a date. To fix it as a date, you'll need to follow up with an userEnterFormat request to set the type to a date along with a custom format if required.
|
|
718
|
+
|
|
343
719
|
#### UI settings
|
|
344
720
|
|
|
345
721
|
Some of the options available in the GAS UI for setting or examining data validation are not available via GAS, and may not be available via Sheets. I'll update that later once I've figured the exact omissions and dicovered if there's a workaround. Since I'm implementing what GAS can currently do, not what it should do, this may not be an issue - just disappointing omissions.
|
|
@@ -354,14 +730,102 @@ Some of the options available in the GAS UI for setting or examining data valida
|
|
|
354
730
|
|
|
355
731
|
This API property controls whether to show a drop down as plain text, or to use a fancy display such as chip or arrow. In the UI the default is true, and the displayStyle is "CHIP". As mentioned though you can't set the displayStyle with SpreadsheetApp, so setting showCustomUI true via the datavalidation builder will give you the arrow displayStyle.
|
|
356
732
|
|
|
357
|
-
In the Apps Script DataValidation builder, setting showCustomUi is achieved via the boolean 2nd argument(known as showDropdown) to requireValueInList() and requireValueInRange().
|
|
733
|
+
In the Apps Script DataValidation builder, setting showCustomUi is achieved via the boolean 2nd argument(known as showDropdown) to requireValueInList() and requireValueInRange().
|
|
358
734
|
|
|
359
735
|
Despite the various defaults, a missing value for these properties returned via the Sheets API always means false, and a missing displayStyle with showCustomUi set to true default is "ARROW".
|
|
360
736
|
|
|
361
|
-
|
|
737
|
+
### Enums
|
|
362
738
|
|
|
363
739
|
All Apps Script enums are imitated using a seperate class 'newFakeGasenum()'. A complete write up of that is in [fakegasenum](https://github.com/brucemcpherson/fakegasenum). The same functionality is also available as an Apps Script library if you'd like to make your own enums over on GAS just like you find in Apps Script.
|
|
364
740
|
|
|
741
|
+
### Auth
|
|
742
|
+
|
|
743
|
+
Sometime between v144 and v150 of googleapis library, it appeared to become mandatory to include the project id in the auth pattern for API clients. Since we get the project id from the ADC, we actually have to do double auths. One to get the project id (which is async), and another to get an auth with the scopes required for the sheets, drive etc client (which is not async). All this now taken care of during the init phase, so look at an existing getauthenticated client function for how if you are adding a new service,
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
## Some experiences with using Gemini code assist
|
|
747
|
+
|
|
748
|
+
I tried using Gemini to generate the code and test cases for a number of method types. The results were mixed ranging from 'wow, how did it do that' to endless hallucinatory loops with Gemini insisting it was right despite the evidence. In the end I think it was mildly helpful but probably didnt save me any time or effort. It was just a different kind of effort.
|
|
749
|
+
|
|
750
|
+
Another annoyance is after deep sessions of back and forwards, code assist is generally unable to make the changes automatically and often reverts to an empty gray sidebar - which means you have to start again. Recalling the history doesn't necessarily reinstate where you were.
|
|
751
|
+
|
|
752
|
+
I also dislike the habit gemini has of 'mansplaining' back to me the answer I've just provided to correct some of it's code.
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
#### range.banding
|
|
756
|
+
|
|
757
|
+
This was a fairly convoluted section. I used gemini code assist heavily on this to do the legwork and all in all it made a pretty decent job of it, although with the endlessly repeated updates and test refactoring it took longer from start to finish than I would have expected it to take had I done it from scratch manually as all the previous classes. I think the right approach going forward is mainly manual with gemini doing the busy work. The tests Gemini came up with were aldo far from exhaustive, and pretty much ignored edge cases, so it needed additional requests to add more robust tests. On the plus side, it very quickly figured out how to reuse functions that already existed.
|
|
758
|
+
|
|
759
|
+
#### developer meta data
|
|
760
|
+
|
|
761
|
+
As per range.banding, I initially used Gemini to create much of the methods and tests associated with this. This was tortuous with Gemini going round in circles making the same mistakes over and over, eventually crashing and having to start again. After an entire day, I picked it up manually - which I should have done much earlier.Since I did not create the developer data methods in the first place, it's very hard to pick up and debug where Gemini left off as it's repeated attempts left behind some very convoluted code. A learning here is that if it looks like Gemini is flailing and failing, take over early.
|
|
762
|
+
|
|
763
|
+
As an aside, I find the implementation of developer meta data very messy and inconsistent with the usual Apps Script services. I believe that regular Apps Script developers will find it unfamiliar, restrictive and intimidating (which is maybe why it never really caught on)
|
|
764
|
+
|
|
765
|
+
#### grouping and collapsing
|
|
766
|
+
|
|
767
|
+
Gemini took me down a rabbit hole on this one, where it kept forgetting that the objective was to have the fake environment behave as Apps Script. Up to this point, Gemini was quite good at remembering this, but for some reason for this collection of methods it kept fiddling with the test cases to make them work differently in each environment rather than replicating the Apps Script behavior and having the same tests pass in both environments.
|
|
768
|
+
|
|
769
|
+
In particular it started to believe that the Apps Script environment was not atomic and to try to modify tests with Utilities.sleep everywhere and many other false avenues.
|
|
770
|
+
|
|
771
|
+
Quite often all that is needed is for you to read the documentation yourself, undo the unnecessary labyrinth of gemini changes, and paste a copy of the documentation into the gemini context to get it back on track. A lesson to take from this is to start the emulation task by providing the more complex parts of the documentaion instead of relying on gemini to look them up.
|
|
772
|
+
|
|
773
|
+
#### pivot tables
|
|
774
|
+
|
|
775
|
+
There are many classes and methods required to support pivot table, so I decided to try to have gemini build them all. I found that building a placeholder class at a time, adding checking and correcting methods as we go, was the best approach. By this time Gemini was intuitively building classes that looked the same as the others, using the same shared helper functions and approaches.
|
|
776
|
+
|
|
777
|
+
Gemini tends to have its own opinion about which methods should exist in apps script classes and this is almost always wrong. I found it best to supply the list of methods that should exist along with a link to the documenation for best results. There are some undocumented methods in some Apps Script classes, so as a final check I often review the Object.keys() of an instance to see if any are missing from the documentation.
|
|
778
|
+
|
|
779
|
+
Despite this Gemini will often create unknown methods, miss known ones, and attempt to reference private functions and methods when creating test cases. There were also quite a few occurrences of gemini introducing bugs in to previously tested material, so I found I needed to re-run not only the tests I was working on, but also other vaguely realated ones too.
|
|
780
|
+
|
|
781
|
+
In summary, Gemini has achieved a lot of good work with this collection of classes, however I don't yet feel completetly confident that we have a completely robust set of implementations. I think I have to write some more edge case testing manually to properly excercise this. The tests created by Gemini are relatively superficial.
|
|
782
|
+
|
|
783
|
+
On the other hand, using these techniques meant that we got the entire pivot table collection of classes and tests to this point in about a day, mainly tracking down filter hallucinations. It would probably have taken me a few days to do it all manually.
|
|
784
|
+
|
|
785
|
+
As a general rule, once Gemini starts talking about making sweeping changes and suggesting that the Sheets API has bugs that is causing everything to collapse, it's time to start a new session. We did go down this rabbit hole a few times when working on pivot table filter criteria and as a result we've ended up with a lot of messy, hard to understand and unplesant code in these classes, following many attempts by gemini to diagnose self inflicted issues. I may have to back and take out some of the redundancy at some point.
|
|
786
|
+
|
|
787
|
+
#### Datasource
|
|
788
|
+
|
|
789
|
+
These have been basicly implemented, but remain untested in any way. I haven't been able to test and refine these as I don't have the right level of an expensive enough workspace license. Will come back to that at a later date - TODO.
|
|
790
|
+
|
|
791
|
+
#### r1c1 style ranges
|
|
792
|
+
|
|
793
|
+
The Sheets API doesn't know about these, so all r1c1 style methods such as setFormulasR1C1 include a conversion to a regular range to be able to communicate with the underlying sheets API. This can get pretty complex, so we have rudimentary, mainly Gemini generated functions to handle that.
|
|
794
|
+
|
|
795
|
+
#### Intial verdict on using Gemini to generate some of this stuff
|
|
796
|
+
|
|
797
|
+
I'm torn. On the one hand, it's been great at doing busy work like writing test cases and detecting dependencies that I might otherwise have missed. It can often be pretty good at refactoring/renaming things. On the other hand, if it gets it wrong, it's very hard to get it back on track as it tries bury itself deeper and deeper into previous misconceptions. It also has huge difficulty in updating large files no matter the detailed guidance. The usual end game is to restart a fresh context and/or copy and paste the content into a file you create manually.
|
|
798
|
+
|
|
799
|
+
There were ocassions when the content Gemini provided content to be copied and pasted that was invalid syntax, or worse, dropped lines of code in sections it didn't plan to make any changes. In particular, code that had something like `ob[method](args)` was regularily truncated to just `ob`
|
|
800
|
+
|
|
801
|
+
Overall it saves some time, for sure. However, the result is often suboptimal, wordy, lacking in reusability and not something I would be be happy to put my name to. From a coder perspective, the role becomes one of repetetive specification, debugging, checking and testing, while failing to develop a deep understanding of the work in hand. I like coding, so from a satisfaction perspective, I'm not entirely convinced yet. I've found it's very impressive when creating small, standalone scripts but deteriorates rapidly both in speed and effectiveness as the codebase and dependencies grows. There's a point at which it becomes more trouble than it's worth.
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
## Testing
|
|
805
|
+
|
|
806
|
+
I recommend you use the test project included in the repo if you want to do some tests. It uses a Fake DriveApp service to excercise Auth etc. Just change the fixtures in your own environments by following the instructions in [setup-env.md](https://github.com/brucemcpherson/gas-fakes/blob/main/setup-env.MD), then `npm i && npm test`.
|
|
807
|
+
|
|
808
|
+
It's not necessary to run these tests, but if you are a collaborator and want to add some additional methods, you need to create a test section and always run a full set of tests before creating a merge request to ensure your changes havent broken anything. At the time of writing there are about 4,000 active tests.
|
|
809
|
+
|
|
810
|
+
Note that I use a [unit tester](https://ramblings.mcpher.com/apps-script-test-runner-library-ported-to-node/) that runs in both GAS and Node, so the exact same tests will run in both environments. There are some example tests in the repo. Each test has been proved on both Node and GAS. There's also a shell (togas.sh) which will use clasp to push the test code to Apps Script.
|
|
811
|
+
|
|
812
|
+
Each test can be run individually (for example `npm run testdrive`) or all with `npm test`
|
|
813
|
+
|
|
814
|
+
Test settings and fixtures are in the .env file. Some readonly files are publicly shared and can be left with the example value in .env-template. Most files which are written are created and deleted afterwards on successful completion. They will be named something starting with --. In case of failures you may need to delete these yourself. If you want to preserve the testfiles it creates doing a test session, just set the CLEAN parameter in .env to 0.
|
|
815
|
+
|
|
816
|
+
### Pushing to GAS
|
|
817
|
+
|
|
818
|
+
The script uses togas.sh will move the test scripts to GAS with clasp - just set the `SOURCE` and `TARGET` folders in the TOGAS script. Make sure you have an `appsscript.json` manifest in the `SOURCE` folder, as **gas-fakes** reads that to handle OAuth on Node.
|
|
819
|
+
|
|
820
|
+
You can write your project to run on Node and call GAS services, and it will also run on the GAS environment with no code changes, except on the Node side you have this one import
|
|
821
|
+
|
|
822
|
+
```sh
|
|
823
|
+
// all the fake services are here
|
|
824
|
+
import '@mcpher/gas-fakes/main.js'
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
togas.sh will remove imports and exports on the way to apps script, which doesnt support them.
|
|
828
|
+
|
|
365
829
|
## Help
|
|
366
830
|
|
|
367
831
|
As I mentioned earlier, to take this further, I'm going to need a lot of help to extend the methods and services supported - so if you feel this would be useful to you, and would like to collaborate, please ping me on [bruce@mcpher.com](mailto:bruce@mcpher.com) and we'll talk.
|
|
@@ -369,4 +833,4 @@ As I mentioned earlier, to take this further, I'm going to need a lot of help to
|
|
|
369
833
|
## Translations and writeups
|
|
370
834
|
|
|
371
835
|
- [mcpher](https://ramblings.mcpher.com/a-proof-of-concept-implementation-of-apps-script-environment-on-node/)
|
|
372
|
-
- [Russian version](README.RU.md) ([credit Alex Ivanov](https://github.com/oshliaer))
|
|
836
|
+
- [Russian version](README.RU.md) ([credit Alex Ivanov](https://github.com/oshliaer)) - needs updating
|