Inicio Crear Google Form desde Google Sheet
Artículo
Cancelar

Crear Google Form desde Google Sheet

Video explicativo de como se usa el script

Script modificado

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var IS_REQUIRED = false;

function onOpen() {
  var menu = SpreadsheetApp.getUi().createMenu('Forms');
  menu.addItem('CREATE TEMPLATE', 'createTemplate').addToUi();
  menu.addItem('CREATE FORM', 'createForm').addToUi();
  menu.addItem('CREATE FORMS', 'createForms').addToUi();
}


function createTemplate() {
  var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  s.deleteColumns(17, 8);
  s.deleteRows(100,900);
  s.setColumnWidth(1, 110);
  s.setColumnWidth(2, 400);
  s.setColumnWidths(3, 16, 110);
  s.setFrozenColumns(2);
  s.setFrozenRows(3);
  s.getRange('A4:A100').setDataValidation(SpreadsheetApp.newDataValidation()
  .setAllowInvalid(false)
  .requireValueInList(['ACCEPTANCE', 'CHECKBOX', 'CHECKGRID', 'CHOICE', 'DATE', 'GRID', 'IMAGE1', 'IMAGE2', 'LIST', 'PAGE', 'PARAGRAPH', 'SCALE', 'SECTION', 'TEXT', 'TIME', 'VIDEO'], true)
  .build());
  s.getRange('A1:A100').setFontWeight('bold').setHorizontalAlignment('center');
  s.getRange('A1').setValue('EXERCISE');
  s.getRange('A2').setValue('DESCRIBE');
  s.getRange('A3').setValue('TYPE');
  s.getRange('C1').setValue('FOLDER ID:').setFontWeight('bold').setHorizontalAlignment('right');
  s.getRange('E1').setValue('PUBLIC URL:').setFontWeight('bold').setHorizontalAlignment('right');
  s.getRange('G1').setValue('COPY FORM:').setFontWeight('bold').setHorizontalAlignment('right');
  s.getRange('H1').setDataValidation(SpreadsheetApp.newDataValidation().setAllowInvalid(false).requireValueInList(['YES'], true).build());
  s.getRange('I1').setValue('LINK COPY:').setFontWeight('bold').setHorizontalAlignment('right');
  s.getRange('K1').setValue('SHUFFLE:').setFontWeight('bold').setHorizontalAlignment('right');
  s.getRange('L1').setDataValidation(SpreadsheetApp.newDataValidation().setAllowInvalid(false).requireValueInList(['YES'], true).build());
  s.getRange('B3').setValue('QUESTION').setFontWeight('bold').setHorizontalAlignment('center');
  s.getRange('C3').setValue('INSTRUCTIONS').setFontWeight('bold').setBackground('#c4daea').setHorizontalAlignment('center');
  s.getRange('D3').setValue('POINTS').setFontWeight('bold').setBackground('#ffff66').setHorizontalAlignment('center');
  s.getRange('E3').setValue('TEXT TRUE').setFontWeight('bold').setBackground('#00ffff').setHorizontalAlignment('center');
  s.getRange('F3').setValue('TEXT FALSE').setFontWeight('bold').setBackground('#00ffff').setHorizontalAlignment('center');
  s.getRange('G3').setValue('LINK').setFontWeight('bold').setBackground('#00ffff').setHorizontalAlignment('center');
  s.getRange('H3').setValue('LINK TEXT').setFontWeight('bold').setBackground('#00ffff').setHorizontalAlignment('center');
  s.getRange('I3:R3').setValue('OPTION').setFontWeight('bold').setBackground('#d4dee5').setHorizontalAlignment('center');
  s.getRange('E4:H100').setBackground('#00ffff');
  s.getRange('I4:R100').setBackground('#d4dee5');
  s.getRange('C4:C100').setBackground('#c4daea');
  s.getRange('D4:D100').setBackground('#ffff66');
};


function createForm() {
  var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  body(s);   
}


function createForms() {
  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (var k=0; k<sh.length; k++) {  
    var s = sh[k];
    body(s);
  }
} 


function body(s) {
var r = s.getDataRange();
var nr = r.getNumRows();
var nc = r.getNumColumns();
var lr = s.getLastRow();
var lc = s.getLastColumn();
var d = r.getValues();
var fol = DriveApp.getFolderById(d[0][3]);

if (d[0][7] === 'YES') {
var fm = DriveApp.getFileById(d[0][9]).makeCopy(d[0][1], fol);
var id = fm.getId();
var f = FormApp.openById(id);
} else {
var fm = FormApp.create(d[0][1]);
var id = fm.getId();
var f = FormApp.openById(id);
}

f.setDescription(d[1][1]);
f.setIsQuiz(true);  

var ur = f.getPublishedUrl();
s.getRange('F1').setValue(ur);


var file = DriveApp.getFileById(id);
fol.addFile(file);
DriveApp.getRootFolder().removeFile(file);
  

for(var x=0;x<nr;x++){ // Beginning of for loop with x
  var i = d[x][0];
  var cr = 1 + x;
  var ro = s.getRange(cr, 8, 1, 10);
  var op = ro.getValues();
    if(i==''){continue;}

    else if (i =='CHOICE') {
        var arr = [];
        
        if (d[0][11] == "YES"){ var its = f.getItems();
          for (var w = 0; w < its.length; w += 1){ var ite = its[w]; if (ite.getTitle() === "CHOICE"){ var q = ite.asMultipleChoiceItem().duplicate(); }}
        } else { var q = f.addMultipleChoiceItem(); }
        
        q.setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
        
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
        
        for (var ccc = 8; ccc<nc; ccc++) {
          var cu = 1 + ccc;
          if (s.getRange(cr,cu,1,1).getValue() !== '' && s.getRange(cr,cu,1,1).getBackground() === "#00ff00") {var q1 = q.createChoice(d[x][ccc], true); arr.push(q1);} 
          else if (s.getRange(cr,cu,1,1).getValue() !== '' && s.getRange(cr,cu,1,1).getBackground() !== "#00ff00") {var q1 = q.createChoice(d[x][ccc], false); arr.push(q1);}
        }
        
        q.setChoices(arr);
        
        if (d[x][4] !== ""){
          var correctFeedback = FormApp.createFeedback().setText(d[x][4]).build();
          q.setFeedbackForCorrect(correctFeedback);
        }
        if (d[x][5] !== ""){
          var incorrectFeedback = FormApp.createFeedback().setText(d[x][5]).addLink(d[x][6],d[x][7]).build();
          q.setFeedbackForIncorrect(incorrectFeedback);
        } 
    }
    
    else if (i =='LIST') {
        var arr = [];
        
        if (d[0][11] == "YES"){ var its = f.getItems();
          for (var w = 0; w < its.length; w += 1){ var ite = its[w]; if (ite.getTitle() === "LIST"){ var q = ite.asListItem().duplicate(); }}
        } else { var q = f.addListItem(); }
        
        q.setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
        
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
  
        for (var ccc = 8; ccc<nc; ccc++) {
          var cu = 1 + ccc;
          if (s.getRange(cr,cu,1,1).getValue() !== '' && s.getRange(cr,cu,1,1).getBackground() === "#00ff00") {var q1 = q.createChoice(d[x][ccc], true); arr.push(q1);} 
          else if (s.getRange(cr,cu,1,1).getValue() !== '' && s.getRange(cr,cu,1,1).getBackground() !== "#00ff00") {var q1 = q.createChoice(d[x][ccc], false); arr.push(q1);}
        }
        
        q.setChoices(arr);
        
        if (d[x][4] !== ""){
          var correctFeedback = FormApp.createFeedback().setText(d[x][4]).build();
          q.setFeedbackForCorrect(correctFeedback);
        }
        if (d[x][5] !== ""){
          var incorrectFeedback = FormApp.createFeedback().setText(d[x][5]).addLink(d[x][6],d[x][7]).build();
          q.setFeedbackForIncorrect(incorrectFeedback);
        } 
    }
  
    else if (i =='CHECKBOX') {
        var arr = [];
        
        if (d[0][11] == "YES"){ var its = f.getItems();
          for (var w = 0; w < its.length; w += 1){ var ite = its[w]; if (ite.getTitle() === "CHECKBOX"){ var q = ite.asCheckboxItem().duplicate(); }}
        } else { var q = f.addCheckboxItem(); }
        
        q.setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
       
       if (d[x][3] !== '') {q.setPoints(d[x][3])}
        
        for (var ccc = 8; ccc<nc; ccc++) {
          var cu = 1 + ccc;
          if (s.getRange(cr,cu,1,1).getValue() !== '' && s.getRange(cr,cu,1,1).getBackground() === "#00ff00") {var q1 = q.createChoice(d[x][ccc], true); arr.push(q1);} 
          else if (s.getRange(cr,cu,1,1).getValue() !== '' && s.getRange(cr,cu,1,1).getBackground() !== "#00ff00") {var q1 = q.createChoice(d[x][ccc], false); arr.push(q1);}
        }
        
        q.setChoices(arr);
        
        if (d[x][4] !== ""){
          var correctFeedback = FormApp.createFeedback().setText(d[x][4]).build();
          q.setFeedbackForCorrect(correctFeedback);
        }
        if (d[x][5] !== ""){
          var incorrectFeedback = FormApp.createFeedback().setText(d[x][5]).addLink(d[x][6],d[x][7]).build();
          q.setFeedbackForIncorrect(incorrectFeedback);
        } 
    }
    
    else if (i =='GRID') {
        // Rows (second row)
        var ro2 = s.getRange(cr+1, 8, 1, 11);
        var op2 = ro2.getValues();
        var arr1 = []; 
        for (q=0; q<op2[0].length; q++){ 
          if (op2[0][q] !== '') {arr1.push(op2[0][q]);} 
        }
        // Columns (first row)
        var arr2 = []; 
        for (q=0; q<op[0].length; q++){ 
          if (op[0][q] !== '') {arr2.push(op[0][q]);} 
        }
        f.addGridItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED).setRows(arr1).setColumns(arr2);
    }
    
    else if (i =='CHECKGRID') {
        // Rows (second row)
        var ro2 = s.getRange(cr+1, 8, 1, 11);
        var op2 = ro2.getValues();
        var arr1 = []; 
        for (q=0; q<op2[0].length; q++){ 
          if (op2[0][q] !== '') {arr1.push(op2[0][q]);} 
        }
        // Columns (first row)
        var arr2 = []; 
        for (q=0; q<op[0].length; q++){ 
          if (op[0][q] !== '') {arr2.push(op[0][q]);} 
        }
        f.addCheckboxGridItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED).setRows(arr1).setColumns(arr2);
    }
    
    else if (i =='TEXT') {
        var q = f.addTextItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
    }
    
    else if (i =='PARAGRAPH') {
        var q = f.addParagraphTextItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
    }
      
    else if (i =='SECTION') {
        f.addSectionHeaderItem().setTitle(d[x][1]).setHelpText(d[x][2]);
    }
    
    else if (i =='PAGE') {
        f.addPageBreakItem().setTitle(d[x][1]).setHelpText(d[x][2]);
    }
    
    else if (i =='IMAGE1') {
        var img = UrlFetchApp.fetch(d[x][6]); 
        f.addImageItem().setTitle(d[x][1]).setHelpText(d[x][2]).setImage(img).setAlignment(FormApp.Alignment.CENTER).setWidth(800);
    }
        
    else if (i =='IMAGE2') {
        var file = DriveApp.getFileById(d[x][6]);
        f.addImageItem().setTitle(d[x][1]).setHelpText(d[x][2]).setImage(file).setAlignment(FormApp.Alignment.CENTER).setWidth(800);
    }
    
    else if (i =='VIDEO') {
        f.addVideoItem().setTitle(d[x][1]).setHelpText(d[x][2]).setVideoUrl(d[x][6]).setAlignment(FormApp.Alignment.CENTER).setWidth(800);
    }
         
    else if (i =='SCALE') {
        var q = f.addScaleItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED).setLabels(d[x][6], d[x][7]).setBounds(d[x][4], d[x][5]);
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
    } 
    
    else if (i =='TIME') {
        var q = f.addTimeItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
    }
   
    else if (i =='DATE') {
        var q = f.addDateItem().setTitle(d[x][1]).setHelpText(d[x][2]).setRequired(IS_REQUIRED);
        if (d[x][3] !== '') {q.setPoints(d[x][3])}
    }
    
    else if(i =='ACCEPTANCE'){
        var item = f.addMultipleChoiceItem();
        var goSubmit = item.createChoice('YES', FormApp.PageNavigationType.SUBMIT);
        var goRestart = item.createChoice('NO', FormApp.PageNavigationType.RESTART);     
          item.setRequired(IS_REQUIRED);
          item.setTitle(d[x][1]);
          item.setHelpText(d[x][2]);
          item.setChoices([goSubmit,goRestart]);   
    } 
   
 } // End of principle for loop with x
 
    var iti = f.getItems();
      for (var y = 0; y < iti.length; y += 1){
        var ito = iti[y];
        if (ito.getTitle() === "CHOICE"){ f.deleteItem(ito); } 
        else if (ito.getTitle() === "LIST"){ f.deleteItem(ito); } 
        else if (ito.getTitle() === "CHECKBOX"){ f.deleteItem(ito); }
      }
  
} // End of entire scipt

Bibliografía

Este artículo está licenciado bajo CC BY 4.0 por el autor.

Práctica: WLAN en Packet Tracer

Instalar OMV 6 en Debian 11 (Bullseye) en mini PC chino