- Регистрация
- 30.07.2013
- Сообщения
- 65
- Благодарностей
- 76
- Баллы
- 18
Замените "КЛЮЧ" на произвольный.
C#:
const apiKey = "КЛЮЧ";
function doPost(e) {
if (e.parameter.key !== apiKey) {
return ContentService.createTextOutput("Unauthorized").setMimeType(ContentService.MimeType.TEXT);
}
if (!e.postData.contents) {
throw new Error("Нет данных для обработки.");
}
const data = JSON.parse(e.postData.contents);
const row = data.row || 1;
const col = data.col || 1;
const value = data.value || "";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(row, col).setValue(value);
return ContentService.createTextOutput("Success");
}
function doGet(e) {
if (e.parameter.key !== apiKey) {
return ContentService.createTextOutput("Unauthorized").setMimeType(ContentService.MimeType.TEXT);
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if (e.parameter.search) {
const searchText = e.parameter.search;
const data = sheet.getDataRange().getValues();
for (let i = 0; i < data.length; i++) {
if (data[i].includes(searchText)) {
return ContentService.createTextOutput(JSON.stringify({ found: "yes", row: i + 1 }));
}
}
return ContentService.createTextOutput(JSON.stringify({ found: "no" }));
} else if (e.parameter.row && e.parameter.col) {
const row = parseInt(e.parameter.row, 10);
const col = parseInt(e.parameter.col, 10);
const value = sheet.getRange(row, col).getValue();
return ContentService.createTextOutput(value);
} else if (e.parameter.action === "get_row_count") {
const rowCount = sheet.getLastRow();
return ContentService.createTextOutput(rowCount.toString());
} else {
return ContentService.createTextOutput("Некорректный запрос.");
}
}
Шаги:
1. Создание Таблицы - открываем Google Drive и создаем новую Таблицу.
2. Настройка Apps Script
- В меню «Расширения» выбираем «Apps Script».
- Добавляем код (выше) для работы со скриптом и создаем уникальный ключ доступа.
- Выполняем развертывание как веб-приложение, разрешаем доступ для всех пользователей.
- Копируем полученную ссылку на веб-приложение и ваш ключ
- Добавляем их в настройки проекта.
- Запускаем программу
- Проверяем, что новые строки добавляются корректно.
Вложения
-
38,3 КБ Просмотры: 15
Последнее редактирование модератором: