11/**
22 * @name ScriptSync
3- * @version 1.0
3+ * @version 2.0.4
44 * @description This script performs an update,
55 * adding new files from the template project
66 * to the current user script.
@@ -32,171 +32,6 @@ function getScriptContent(scriptId) {
3232}
3333
3434
35- /**
36- * Sending the new contents of the script file.
37- *
38- * @name _
39- * @param {string } scriptId Id of the script file that needs to be updated.
40- * @param {ScriptJson } updatedContent New content of the script file.
41- *
42- * @return {boolean } Update result.
43- */
44- function updateScriptContentV3 ( scriptId , updatedContent ) {
45- const url = `https://www.googleapis.com/upload/drive/v2/files/${ scriptId } ?uploadType=media` ;
46- const options = {
47- method : "put" ,
48- headers : {
49- Authorization : "Bearer " + ScriptApp . getOAuthToken ( )
50- } ,
51- muteHttpExceptions : false ,
52- contentType : "application/vnd.google-apps.script+json" ,
53- payload : JSON . stringify ( updatedContent )
54- } ;
55-
56- let result = false ;
57- var response ;
58-
59- try {
60- response = UrlFetchApp . fetch ( url , options ) ;
61- const status_code = response . getResponseCode ( ) ;
62- if ( status_code == 200 ) {
63- L ( "The contents of the script file have been successfully updated." ) ;
64- result = true ;
65- } else {
66- LW ( "Result is not ordinary. Status code:" , status_code ) ;
67- LW_ ( JSON . stringify ( response . getContentText ( ) ) ) ;
68- }
69- } catch {
70- LE ( "An error occurred while updating the contents of the script file." ) ;
71- }
72-
73- return result ;
74- }
75-
76-
77- /**
78- * Retrieves source of file inside the script file.
79- *
80- * @name _
81- * @param {string } scriptId Id of the script file that needs to be updated.
82- * @param {string } fileName The name of the Id of the script file that needs to be updated.
83- * @param {boolean } returnSourceOnly **optional**: New content of the script file. Default - true.
84- * @throws {Error } File was not found inside the template script.
85- *
86- * @return {EntityFileData|string } If `returnSourceOnly` is true, returns the source property of
87- * the file data. Otherwise, returns the entire file data.
88- */
89- function getSourceDataFromTemplate ( scriptId , fileName , returnSourceOnly = true ) {
90- if ( ! fileName && ! scriptId ) throw new Error ( "File name is not defined." ) ;
91-
92- const scriptContent = getScriptContent ( scriptId ) ;
93-
94- // Find the file inside the json by its name
95- var entitiesData = scriptContent . files . find ( file => {
96- return file . name === fileName
97- } ) ;
98-
99- // Check if the file is found
100- if ( ! entitiesData ) {
101- throw new Error ( `No such the file name: '${ fileName } '` ) ;
102- } else {
103- if ( ! entitiesData . source ) {
104- LW ( `File '${ fileName } ' on the template scpipt is blank.` ) ;
105- if ( returnSourceOnly ) entitiesData . source = "// No data." ;
106- }
107- }
108-
109- return returnSourceOnly ? entitiesData . source : ( delete entitiesData . id , entitiesData ) ;
110- }
111-
112-
113- /**
114- * Replaces the source code in the file inside the target script with a new one.
115- *
116- * The function only prepares a new script file without making changes!
117- *
118- * @name _
119- * @param {string } scriptId Target Id of the script file that needs to be updated.
120- * @param {string } fileName The filename inside the script file that needs to be updated.
121- * @param {string } newJsonSource New source content data from template script file (EntityFileData.source).
122- * @throws {Error } File was not found inside the target script or the source was empty.
123- *
124- * @return {ScriptJson } New ScriptJson with the updated source code.
125- */
126- function modifySourceDataObject ( scriptId , fileName , newJsonSource ) {
127- if ( ! scriptId || ! fileName || ! newJsonSource ) throw new Error ( "Parameter is not defined." ) ;
128-
129- var scriptContent = getScriptContent ( scriptId ) ;
130-
131- // Find the file inside the json by its name
132- var entitiesData = scriptContent . files . find ( file => {
133- return file . name === fileName ;
134- } ) ;
135-
136- // Check if the file is found
137- if ( entitiesData ) {
138- // Modify the contents of the "source" property in json_data
139- entitiesData . source = newJsonSource ;
140- L ( "The source code of the '%s' file will be updated" , fileName ) ;
141- } else {
142- let error_msg = `File '${ fileName } ' was not found in entries of the ID: ${ scriptId } or file is blank.` ;
143- error_msg += "\nUse 'IO_AddNewFile' method instead this." ;
144- throw new Error ( error_msg ) ;
145- }
146-
147- return scriptContent ;
148- }
149-
150-
151- /**
152- * Replaces the target script file with a new one.
153- *
154- * The function only prepares a new script file without making changes!
155- *
156- * @name _
157- * @param {string } scriptId Target Id of the script file that needs to be updated.
158- * @param {string } savedFileName The filename inside the script file that needs to be updated.
159- * @param {EntityFileData } newJson The new file that will be added to target script file.
160- *
161- * @return {ModResult } Object with a new ScriptJson and added file.
162- */
163- function modifyOrAddFileToDataObject ( scriptId , savedFileName , newJson ) {
164- if ( ! scriptId || ! savedFileName || ! newJson ) throw new Error ( "Parameter is not defined." ) ;
165-
166- let result = { result : false , isAdd : undefined , data : "" } ;
167-
168- try {
169- var scriptContent = getScriptContent ( scriptId ) ;
170-
171- // Find INDEX of the file inside the json by its name
172- const fileIndex = scriptContent . files . findIndex ( file => {
173- return file . name === savedFileName ;
174- } ) ;
175-
176- if ( fileIndex === - 1 ) {
177- // If file is not found, create a new entry
178- newJson . name = savedFileName ;
179- scriptContent . files . push ( newJson ) ;
180- result . isAdd = true ;
181- L ( "File '%s' will be added" , savedFileName ) ;
182- } else {
183- // Modify the contents of the "source" property in json_data
184- scriptContent . files [ fileIndex ] . source = newJson . source ;
185- result . isAdd = false ;
186- L ( "File '%s' will be updated" , savedFileName ) ;
187- }
188-
189- result . data = scriptContent ;
190- result . result = true ;
191-
192- } catch ( e ) {
193- LE ( e . message ) ;
194- }
195-
196- return result ;
197- }
198-
199-
20035/**
20136 * Returns a list of the script files.
20237 *
@@ -207,7 +42,7 @@ function modifyOrAddFileToDataObject(scriptId, savedFileName, newJson) {
20742 * is an error fetching the content.
20843 * @return {Array.<ListItem> } Array of objects.
20944 */
210- function IO_GetScriptFiles ( scriptId ) {
45+ function getScriptFiles ( scriptId ) {
21146 var scriptFiles = [ ] ;
21247 try {
21348 scriptId = scriptId || ScriptApp . getScriptId ( ) ;
@@ -236,124 +71,22 @@ function IO_GetScriptFiles(scriptId) {
23671 * is an error fetching the content.
23772 * @return {Array.<ListItem> } Array of objects.
23873 */
239- function IO_GetTemplateScriptFiles ( launchedFromScript = false ) {
74+ function getTemplateScriptFiles ( launchedFromScript = false ) {
24075 if ( ! launchedFromScript ) throw new Error ( "Parameter is not defined." ) ;
241- return IO_GetScriptFiles ( getRemoteScriptId_ ( ) ) ;
76+ return getScriptFiles ( getRemoteScriptId_ ( ) ) ;
24277}
24378
24479
24580/**
246- * Updates any file of the current script from a template script file.
247- *
81+ * Saves file with code examples in the user script.
24882 * **The function makes changes to the current script file!**
249- *
250- * @public LIB User execution
251- * @param {string } fromFileName Filename (in template script) from which the new data is copied.
252- * @param {string } toFileName **optional**: Filename (in this script) to which the new data is being copied.
253- * If empty, the file name specified in the remote script is used.
254- * @param {string } templateScriptId **optional**: Script file_id from where to copy the file. Default value is
255- * defined by the library if it is empty.
256- * You can get ScriptId: `ScriptApp.getScriptId()`.
257- *
258- * @return {boolean } Result of the function execution.
259- */
260- function IO_UpdateFile ( fromFileName , toFileName , templateScriptId ) {
261- if ( ! fromFileName ) {
262- LE ( "Value of 'fromFileName' parameter is not defined." ) ;
263- return false ;
264- }
265-
266- let result = false ;
267- const user_file_name = toFileName || fromFileName ;
268-
269- try {
270- // New project (user) file of the SCRIPT which will be modified
271- const user_script_id = ScriptApp . getScriptId ( ) ;
272-
273- // Template data file
274- const source_script_id = templateScriptId || getRemoteScriptId_ ( ) || '' ;
275- const source_file_name = fromFileName ;
276- var source_file_data = getSourceDataFromTemplate ( source_script_id , source_file_name ) ;
277-
278- // Modified script data of the user_script_id
279- const receiverData = modifySourceDataObject ( user_script_id , user_file_name , source_file_data ) ;
280-
281- if ( receiverData ) {
282- result = updateScriptContentV3 ( user_script_id , receiverData ) ;
283- } else {
284- LE ( "Something went wrong. Check the current script file." ) ;
285- }
286- } catch ( e ) {
287- LE ( e . message ) ;
288- } finally {
289- if ( result ) {
290- L ( "File '%s' succefully updated. Reload the tab." , user_file_name ) ;
291- } else {
292- LW ( "Nothing changed. Please check the source file and permissions." ) ;
293- }
294- }
295-
296- return result ;
297- }
298-
299-
300- /**
301- * Adds a new file from a template to the current script. from another third-party script.
302- * If the file exists, it will be updated.
303- *
304- * **The function makes changes to the current script file!**
305- *
306- * @public LIB User execution
307- * @param {string } fromFileName Filename (in template script) from which the new data is copied.
308- * @param {string } toFileName **optional**: Filename (in this script) to which the new data is being copied.
309- * If empty, the file name specified in the remote script is used.
310- * @param {string } templateScriptId **optional**: Script file_id from where to copy the file. Default value is
311- * defined by the library if it is empty.
312- * You can get ScriptId: `ScriptApp.getScriptId()`.
313- *
314- * @return {boolean } Result of the function execution.
315- *
83+ * @name _
31684 */
317- function IO_AddNewFile ( fromFileName , toFileName , templateScriptId ) {
318- if ( ! fromFileName ) {
319- LE ( "Value of 'fromFileName' parameter is not defined." ) ;
320- return false ;
321- }
322-
323- let result = false ;
324- var receiverData = { } ;
325- const user_file_name = toFileName || fromFileName ;
326-
327- try {
328- // New project (user) file of the SCRIPT which will be modified
329- const user_script_id = ScriptApp . getScriptId ( ) ;
330-
331- // Template data file
332- const source_script_id = templateScriptId || getRemoteScriptId_ ( ) || '' ;
333- const source_file_name = fromFileName ;
334- var source_file_data_j = getSourceDataFromTemplate ( source_script_id , source_file_name , false ) ;
335-
336- // Modified script data of the user_script_id
337- receiverData = modifyOrAddFileToDataObject ( user_script_id , user_file_name , source_file_data_j ) ;
338-
339- // checking
340- if ( receiverData ?. data && receiverData ?. result === true ) {
341- result = updateScriptContentV3 ( user_script_id , receiverData . data ) ;
342- } else {
343- LE ( "Something went wrong. Check the current script file." ) ;
344- }
345- } catch ( e ) {
346- LE ( e . message ) ;
347- } finally {
348- if ( result ) {
349- if ( receiverData . isAdd ) L ( "File '%s' succefully added. Reload the tab." , user_file_name ) ;
350- if ( ! receiverData . isAdd ) L ( "File '%s' succefully updated. Reload the tab." , user_file_name ) ;
351- } else {
352- LW ( "Nothing changed. Please check the source file and permissions." ) ;
353- }
354- }
355-
356- return result ;
85+ function IO_GetSamples ( ) {
86+ const fn = 'examples' ;
87+ const samples = assignTemplate ( getRemoteScriptId_ ( ) ) ;
88+ samples . _AddNewFile ( fn )
89+ . _renameFile ( fn , 'lib_example' , 'gs' )
90+ . _viewChanges ( 10 )
91+ . _commit ( false ) ;
35792}
358-
359-
0 commit comments