File size: 11,073 Bytes
455c04c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Generate Google Apps Script
function generateScript(columns) {
    return `
function doGet(e) {
  try {
    const action = e.parameter.action;
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    switch (action) {
      case 'search':
        return handleSearchGet(sheet, e.parameter.q);
      case 'fetch':
        const startRow = e.parameter.startRow ? parseInt(e.parameter.startRow) : null;
        const endRow = e.parameter.endRow ? parseInt(e.parameter.endRow) : null;
        return handleFetchGet(sheet, startRow, endRow);
      default:
        return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid action.' })).setMimeType(ContentService.MimeType.JSON);
    }
  } catch (error) {
    Logger.log(error);
    return ContentService.createTextOutput(JSON.stringify({ error: error.message })).setMimeType(ContentService.MimeType.JSON);
  }
}

function doPost(e) {
  try {
    if (e.postData.type !== 'application/json') {
      return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid content type. Only JSON is accepted.' })).setMimeType(ContentService.MimeType.JSON);
    }

    const data = JSON.parse(e.postData.contents);
    const action = data.action;
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    switch (action) {
      case 'append':
        return handleAppend(sheet, data.rows);
      case 'search':
        return handleSearch(sheet, data.q);
      case 'update':
        return handleUpdate(sheet, data.row, ${columns.map(col => `data.${col}`).join(', ')});
      case 'delete':
        return handleDelete(sheet, data.row);
      case 'fetch':
        return handleFetch(sheet, data.row, data.endRow);
      default:
        return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid action.' })).setMimeType(ContentService.MimeType.JSON);
    }
  } catch (error) {
    Logger.log(error);
    return ContentService.createTextOutput(JSON.stringify({ error: error.message })).setMimeType(ContentService.MimeType.JSON);
  }
}

function handleAppend(sheet, rows) {
  if (!Array.isArray(rows) || rows.length === 0) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid rows data.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const values = rows.map(row => [${columns.map(col => `row.${col}`).join(', ')}]);
  sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);

  return ContentService.createTextOutput(JSON.stringify({ success: true, rowsAdded: rows.length })).setMimeType(ContentService.MimeType.JSON);
}

function handleSearch(sheet, query) {
  if (!query) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Search query is required.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const data = sheet.getDataRange().getValues();
  const results = data.filter(row => ${columns.map((col, i) => `row[${i}].toLowerCase().includes(query.toLowerCase())`).join(' || ')});

  return ContentService.createTextOutput(JSON.stringify(results)).setMimeType(ContentService.MimeType.JSON);
}

function handleSearchGet(sheet, query) {
  if (!query) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Search query is required.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const data = sheet.getDataRange().getValues();
  const results = data.filter(row => ${columns.map((col, i) => `row[${i}].toLowerCase().includes(query.toLowerCase())`).join(' || ')});

  return ContentService.createTextOutput(JSON.stringify(results)).setMimeType(ContentService.MimeType.JSON);
}

function handleUpdate(sheet, rowIndex, ${columns.join(', ')}) {
  if (!rowIndex || ${columns.map(col => `!${col}`).join(' || ')}) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid update data.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const row = rowIndex + 1;
  if (row > sheet.getLastRow()) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Row index out of range.' })).setMimeType(ContentService.MimeType.JSON);
  }

${columns.map((col, i) => `  sheet.getRange(row, ${i + 1}).setValue(${col});`).join('\n')}

  return ContentService.createTextOutput(JSON.stringify({ success: true })).setMimeType(ContentService.MimeType.JSON);
}

function handleDelete(sheet, rowIndex) {
  if (!rowIndex) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Row index is required.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const row = rowIndex + 1;
  if (row > sheet.getLastRow()) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Row index out of range.' })).setMimeType(ContentService.MimeType.JSON);
  }

  sheet.deleteRow(row);
  return ContentService.createTextOutput(JSON.stringify({ success: true })).setMimeType(ContentService.MimeType.JSON);
}

function handleFetch(sheet, startRow, endRow) {
  const lastRow = sheet.getLastRow();
  startRow = startRow ? startRow + 1 : 1;
  endRow = endRow ? endRow + 1 : lastRow;

  if (startRow > lastRow || endRow > lastRow || startRow > endRow) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid row range.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const data = sheet.getRange(startRow, 1, endRow - startRow + 1, ${columns.length}).getValues();
  return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON);
}

function handleFetchGet(sheet, startRow, endRow) {
  const lastRow = sheet.getLastRow();
  startRow = startRow ? startRow + 1 : 1;
  endRow = endRow ? endRow + 1 : lastRow;

  if (startRow > lastRow || endRow > lastRow || startRow > endRow) {
    return ContentService.createTextOutput(JSON.stringify({ error: 'Invalid row range.' })).setMimeType(ContentService.MimeType.JSON);
  }

  const data = sheet.getRange(startRow, 1, endRow - startRow + 1, ${columns.length}).getValues();
  return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON);
}`.trim();
}

// Generate cURL examples as an object
function generateCurlExamples(columns) {
    const sampleValues = columns.map((col, i) => [col, `Sample ${col} ${i + 1}`]);
    return {
        appendSingle: `
curl -X POST "https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec" \\
-H "Content-Type: application/json" \\
-d '{
  "action": "append",
  "rows": [{
    ${sampleValues.map(([col, val]) => `"${col}": "${val}"`).join(',\n    ')}
  }]
}'`.trim(),
        appendMultiple: `
curl -X POST "https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec" \\
-H "Content-Type: application/json" \\
-d '{
  "action": "append",
  "rows": [
    { ${sampleValues.map(([col, val]) => `"${col}": "${val}"`).join(', ')} },
    { ${sampleValues.map(([col, val]) => `"${col}": "Another ${val}"`).join(', ')} }
  ]
}'`.trim(),
        fetchAll: `
curl "https://script.google.com/macros/s/YOUR_WEB_APP_ID/exec?action=fetch"`.trim(),
        fetchRange: `
curl "https://script.google.com/macros/s/YOUR_WEB_APP_ID/exec?action=fetch&startRow=50&endRow=100"`.trim(),
        search: `
curl "https://script.google.com/macros/s/YOUR_WEB_APP_ID/exec?action=search&q=sample"`.trim(),
        update: `
curl -X POST "https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec" \\
-H "Content-Type: application/json" \\
-d '{
  "action": "update",
  "row": 50,
  ${sampleValues.map(([col, val]) => `"${col}": "Updated ${val}"`).join(',\n  ')}
}'`.trim(),
        delete: `
curl -X POST "https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec" \\
-H "Content-Type: application/json" \\
-d '{
  "action": "delete",
  "row": 100
}'`.trim()
    };
}

// Update previews
function updatePreviews() {
    const columns = Array.from(document.querySelectorAll('.column-input'))
        .map(input => input.value.trim())
        .filter(value => value !== '');
    document.getElementById('codePreview').textContent = generateScript(columns);
    const curlExamples = generateCurlExamples(columns);
    const activeButton = document.querySelector('.curl-button.active');
    const exampleKey = activeButton ? activeButton.getAttribute('onclick').match(/'([^']+)'/)[1] : 'appendSingle';
    document.getElementById('curlExamples').textContent = curlExamples[exampleKey];
}

// Add new column
function addColumn() {
    const container = document.getElementById('columnInputs');
    const div = document.createElement('div');
    div.innerHTML = `
        <div class="flex space-x-2">
            <input type="text" class="column-input flex-1 px-3 py-2 border rounded-md" placeholder="Column name">
            <button onclick="removeColumn(this)" class="bg-red-500 text-white px-2 py-1 rounded hover:bg-red-600">X</button>
        </div>
    `;
    container.appendChild(div);
    updatePreviews();
}

// Remove column
function removeColumn(button) {
    button.parentElement.parentElement.remove();
    updatePreviews();
}

// Tab switching
function showTab(tabId) {
    document.querySelectorAll('.tab-content').forEach(tab => tab.classList.remove('active'));
    document.getElementById(tabId).classList.add('active');
    document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('border-blue-500'));
    event.target.classList.add('border-blue-500');
}

// Show specific cURL example
function showCurlExample(exampleKey) {
    const columns = Array.from(document.querySelectorAll('.column-input'))
        .map(input => input.value.trim())
        .filter(value => value !== '');
    const curlExamples = generateCurlExamples(columns);
    document.getElementById('curlExamples').textContent = curlExamples[exampleKey];
    document.querySelectorAll('.curl-button').forEach(btn => btn.classList.remove('active'));
    event.target.classList.add('active');
}

// Copy code to clipboard
function copyCode() {
    const code = document.getElementById('codePreview').textContent;
    if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(code).then(() => {
            alert('Code copied to clipboard!');
        }).catch(err => {
            console.error('Failed to copy: ', err);
            fallbackCopy(code);
        });
    } else {
        fallbackCopy(code);
    }
}

// Fallback for older browsers or restricted environments
function fallbackCopy(text) {
    const textArea = document.createElement('textarea');
    textArea.value = text;
    document.body.appendChild(textArea);
    textArea.select();
    try {
        document.execCommand('copy');
        alert('Code copied to clipboard!');
    } catch (err) {
        alert('Failed to copy code. Please copy it manually.');
    }
    document.body.removeChild(textArea);
}

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    updatePreviews();
    document.getElementById('columnInputs').addEventListener('input', updatePreviews);
    // Set default active cURL button
    const firstCurlButton = document.querySelector('.curl-button');
    if (firstCurlButton) {
        firstCurlButton.classList.add('active');
    }
});