admin管理员组

文章数量:1122832

I am trying to save user emails with subscribe button on a webapp made through Google Apps Script with access to anyone. This webapp has a email form where user enter their email and post request is made from the button. I want to know if the Google Sheet containing all the emails can be accessed by other users in any way. I want to ensure that the sheet remains private and that no one else can view the emails stored in it.

Below is my doPost() code:

function doPost(e) {
  try { 
    const spreadsheet = SpreadsheetApp.openById("asbasdsdf...");
    const sheet = spreadsheet.getSheetByName("Subscriptions");

    // Check if the sheet exists
    if (!sheet) {
      throw new Error(`The sheet "${sheetName}" does not exist.`);
    }

    // Parse JSON payload
    const data = JSON.parse(e.postData.contents);
    const email = data.email;
    
    // Check if the email already exists in the specified column
    const range = sheet.getRange(1, column, sheet.getLastRow(), 1);
    const existingEmails = range.getValues().flat();
    if (existingEmails.includes(email)) {
      return ContentService.createTextOutput(
        JSON.stringify({ status: "error", message: "This email is already subscribed for " + bookCover })
      ).setMimeType(ContentService.MimeType.JSON);
    }

    // Format current date and time (DD/MM/YYYY, HH:MM:SS)
    const timestamp = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yyyy, HH:mm:ss");

    // Append email and timestamp
    const row = sheet.getLastRow() + 1;
    sheet.getRange(row, column).setValue(email);
    sheet.getRange(row, 3).setValue(timestamp);

    // Success response
    return ContentService.createTextOutput(
      JSON.stringify({ status: "success", message: "Email saved successfully!" })
    ).setMimeType(ContentService.MimeType.JSON);

  } catch (error) {
    return ContentService.createTextOutput(
      JSON.stringify({ status: "error", message: error.message })
    ).setMimeType(ContentService.MimeType.JSON);
  }
}


本文标签: