Skip to content

Commit c4760ec

Browse files
authored
Update README.md
1 parent a8177f8 commit c4760ec

1 file changed

Lines changed: 126 additions & 11 deletions

File tree

README.md

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ In order to use this library, please install this library.
3333

3434
## About scopes
3535

36-
About the install of scopes using the library, this library requires manually installing scopes into the project that installed the library:
36+
About the install of scopes using the library, this library requires installing scopes into the project that installed the library:
3737

3838
- `https://www.googleapis.com/auth/script.external_request`
3939
- `https://www.googleapis.com/auth/drive.scripts`
@@ -42,6 +42,10 @@ About the install of scopes using the library, this library requires manually in
4242

4343
> IMPORTANT: Above 4 scopes are installed in this library. If you want to use Spreadsheets, please install the scopes for it using [Manifests](https://developers.google.com/apps-script/concepts/manifests) to the project installed this library.
4444
45+
## Also
46+
47+
The library and your script template must be shared.
48+
4549
# Methods
4650

4751
<a name="methods"></a>
@@ -65,18 +69,21 @@ About the install of scopes using the library, this library requires manually in
6569
| Method | Description |
6670
| :------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------ |
6771
| assignTemplate | Initializes the class and sets the ID of the template script as the data source. |
68-
| **Operations** |
69-
| AddNewFile | Adds a new file or replaces an existing one within the script. |
70-
| renameFile | Renames a file within the script. |
71-
| createBlankFile | Creates a new blank file within the script. |
72-
| setCustomSource | Sets the content of the source in a file within the script. |
73-
| addFileToUserJson | Adds a file to the user's script. |
7472
| **General** |
7573
| **commit** * | Applies the pending changes made to the script. |
7674
| drop | Discards the local changes made to the script, without reverting a commit. |
7775
| viewChanges | Displays a list of changes that will be applied to the script upon committing. |
7876
| getChanges | Retrieves a list of changes that will be applied to the script upon committing. |
7977
| getTemplateId | Retrieves the current ID of the script's template. |
78+
| **Operations** |
79+
| AddNewFile | Adds a new file or replaces an existing one within the script. |
80+
| renameFile | Renames a file within the script. |
81+
| deleteFile | Deletes a file from the script file object. |
82+
| compareFilesByContent | Compares the content of two files and returns true if they are identical, false otherwise. |
83+
| createBlankFile | Creates a new blank file within the script. |
84+
| setCustomSource | Sets the content of the source in a file within the script. |
85+
| addFileToUserJson | Adds a file to the user's script. |
86+
| getFileFromTemplate | Retrieves a file from the template script file object. |
8087

8188
\* - makes changes to the script file
8289

@@ -104,7 +111,7 @@ appsscript file, json:
104111
{
105112
"userSymbol": "ScriptSync",
106113
"libraryId": "1nUiajCHQReVwWPq7rNAvsIcWvPptmMUSzeytnzVHDpdoxUIvuX0e_reL",
107-
"version": "4"
114+
"version": "5"
108115
}
109116
]
110117
},
@@ -122,7 +129,7 @@ appsscript file, json:
122129

123130
## User deploy.gs:
124131

125-
A sample script is as follows. This sample script copying the library to current project.
132+
A sample script is as follows. This sample script copies code samples from the library to the current project.
126133

127134
```javascript
128135
function samples() {
@@ -147,9 +154,112 @@ function setMyTemplateId() {
147154
}
148155
```
149156

150-
# Also
157+
## Sample scripts
151158

152-
The library and your script template must be shared.
159+
### Sample 1: Copying new files
160+
```javascript
161+
function copyFiles() {
162+
// script_id of the template script
163+
const template_script_id = "your_template_script_id";
164+
165+
// get an array of files to copy from the template
166+
const filesToCopy = ScriptSync.getScriptFiles(template_script_id);
167+
168+
/* Initialize the template script updater */
169+
const updater = ScriptSync.assignTemplate(template_script_id);
170+
171+
filesToCopy.forEach(function(item) {
172+
if (item.file !== 'appsscript') {
173+
// add the file to the updater
174+
updater.AddNewFile(item.file);
175+
}
176+
});
177+
178+
// AND set your own specific name and type
179+
const fileToCopy = 'appsscript';
180+
updater.AddNewFile(fileToCopy, `from_template_${fileToCopy}`, 'html');
181+
182+
// shows the changes (source trimmed to 30 characters)
183+
updater.viewChanges(30);
184+
185+
// apply the changes if there are no errors (using "false" flag)
186+
updater.commit(false);
187+
188+
/*
189+
// OR try to apply the changes anyway
190+
try {
191+
updater.commit(updater.result);
192+
} catch (e) {
193+
console.log(e.message);
194+
}
195+
*/
196+
}
197+
```
198+
199+
### Sample 2: Add custom source
200+
```javascript
201+
function addMySource() {
202+
/* Initialize your template script */
203+
const template_script_id = "your_template_script_id";
204+
const updater = ScriptSync.assignTemplate(template_script_id);
205+
206+
// any working code
207+
const myFunctions = {
208+
def: function defined() {
209+
/* Comments are also included */
210+
console.log("Hello world!");
211+
}
212+
}
213+
214+
// creating a new file & renaming & set custom code
215+
updater.createBlankFile("the_code", "html")
216+
.renameFile("the_code", "sample_code", "gs")
217+
.setCustomSource("sample_code", "gs", myFunctions.def);
218+
219+
// creating a new file & set custom text & shows the changes
220+
updater.createBlankFile("changes", "html")
221+
.setCustomSource("changes", "html", "Today updates: ...")
222+
.viewChanges(30);
223+
224+
// waiting for interruption by user
225+
Utilities.sleep(20000);
226+
227+
// apply the changes with ignore errors (default)
228+
const result = updater.commit();
229+
console.log(result);
230+
}
231+
```
232+
233+
### Sample 3: Updating if any changes
234+
```javascript
235+
function addMySource() {
236+
/* Initialize your template script */
237+
const template_script_id = "your_template_script_id";
238+
const updater = ScriptSync.assignTemplate(template_script_id);
239+
240+
const match = updater.compareFilesByContent("template_code", "script_code");
241+
242+
if (!match) {
243+
updater.AddNewFile("template_code", "script_code")
244+
.createBlankFile("notice", "html")
245+
.setCustomSource("notice", "html", "Your script was updated at ..")
246+
.commit();
247+
}
248+
}
249+
```
250+
251+
# Restrictions
252+
253+
PLEASE EXERCISE CAUTION WHEN RENAMING AND DELETING FILES!
254+
255+
DO NOT EXCEED THE LIMITATIONS SET BY APPS SCRIPT:
256+
- PROVIDE **ONLY UNIQUE FILENAMES**. FOR EXAMPLE, "**file.html**" AND "**file.json**" ARE __NOT UNIQUE NAMES__.
257+
- USE ONLY EXISTING FILE EXTENSIONS: `html`, `gs`, `json`. DO NOT INVENT NEW ONES.
258+
- DO NOT DELETE THE FILE: `appsscript.json`.
259+
260+
FAILURE TO COMPLY WITH THESE CONDITIONS MAY NOT ONLY RESULT IN ERROR CODES 400 OR 500
261+
262+
BUT ALSO RENDER THE SCRIPT FILE INACCESSIBLE. PLEASE BE ATTENTIVE.
153263

154264
# Important
155265

@@ -175,6 +285,11 @@ If you have any questions and commissions for me, feel free to tell me.
175285

176286
# Update History
177287

288+
- v2.0.5 (May 12, 2024)
289+
290+
1. Added new methods:
291+
- File operations: 2 methods of [deleteFile](#methods_class), [compareFilesByContent](#methods_class) and [getFileFromTemplate](#methods_class) were added.
292+
178293
- v2.0.4 (May 12, 2024)
179294

180295
1. Completely rewritten the library code based on classes.

0 commit comments

Comments
 (0)