[8기 AIA2Z] GPT로 여러개의 구글 시트에 권한 부여하는 코드 얻기

안녕하세요 8기 AIA2Z 강의를 수강하는 몽주입니다.

이런 종류의 글을 써본지도 참 오래됐고, 첫 주 강의를 끝까지 듣지 못해 과제를 이렇게 하는게 맞는지도 잘 모르겠네요 ㅎㅎ 그래도 GPT를 활용한 경험을 공유해보고자합니다.

업무에 직접적으로 활용도 하고, chat gpt 외에도 다른 AI 서비스들에 대해 알아가보려는 마음으로 이 과목을 수강했기 때문에 사례발표 등에서 들은 것을 바탕으로 GPT를 탐색해보았습니다.

최근 다양한 구글시트 문서들을 자동화하는 것에 맛이 들려서, 이번에는 한 이메일 계정이 여러 개의 구글 시트에 대한 권한을 가질 수 있도록 하고자 해당 기능을 구현할 앱스스크립트 코드를 물어봤습니다.


질문: Related to this, I have a list of multiple sheets to monitor and I'd like to grant an external account access to the sheets. How can I do it with apps script? (이전에 시트 자동화 관련 질문을 해 둔 상태라 그 맥락을 활용하고자 Related to this 로 질문을 시작했습니다. ‘여러 개의 시트를 모니터링 할 거라서 한 이메일 계정을 여러 시트에 권한을 주려는데, 앱스스크립트로 어떻게 구현하냐?‘ 는 내용입니다.)

답변:

To grant an external account access to multiple Google Sheets using Google Apps Script, you can use the DriveApp service to manage permissions. The script below demonstrates how to add an external user with view access to a list of specified sheets:


function grantAccessToSheets() {
  // Replace with the email of the external user you want to grant access to
  var externalUserEmail = "[email protected]";

  // Replace with an array of sheet IDs or names that you want to share
  var sheetIdentifiers = ["Sheet1", "Sheet2", "Sheet3"];
  
  // Loop through the sheets and grant access
  sheetIdentifiers.forEach(function(sheetIdentifier) {
    var sheet = getSheetByIdOrName(sheetIdentifier);
    if (sheet) {
      grantAccess(sheet.getId(), externalUserEmail);
      Logger.log("Access granted for sheet: " + sheet.getName());
    } else {
      Logger.log("Sheet not found: " + sheetIdentifier);
    }
  });
}

// Helper function to get a sheet by ID or name
function getSheetByIdOrName(sheetIdentifier) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetIdentifier);
  if (!sheet) {
    sheet = ss.getSheetById(sheetIdentifier);
  }
  return sheet;
}

// Helper function to grant view access to a sheet for an external user
function grantAccess(sheetId, userEmail) {
  var sheetFile = DriveApp.getFileById(sheetId);
  sheetFile.addViewer(userEmail);
}

Please note:

  1. Replace "[email protected]" with the email address of the external user to whom you want to grant access.

  2. Replace ["Sheet1", "Sheet2", "Sheet3"] with an array of sheet IDs or names that you want to share.

You can run the grantAccessToSheets function from the Apps Script editor. The script will iterate through the specified sheets and grant view access to the external user. Keep in mind that you need the appropriate permissions to run this script on the sheets.

Remember that granting access to external users may expose sensitive information, so use caution and ensure that you have the necessary permissions and comply with your organization's policies.


이번에는 문득 강의때 언급을 들은 코파일럿이 생각 나서, gpt가 제공한 코드의 주석으로 이것과 같은 역할을 하는 코드를 얻을 수 있을지, 만약 아니라면 주석을 업데이트 해달라는 요청을 했습니다.

질문1:Do you think the Copliot of Github can write codes that result in the same output? If not, return better annotations for Copilot.

답변1: 제가 원했던 코드 주석이 아니라 코드에 대한 전반적인 설명을 해준 것 같습니다.


질문2:Do you think the Copliot of Github can write codes that result in the same output? If not, return better annotations of the code above to get the same output.

이전에 말한 “코드”에 대한 주석을 달라고 요청했습니다.


답변2: 코파일럿에 대한 설명과 이 코드가 코파일럿에 쓰일 수 있을지 설명합니다. 지금 생각해보니 Do you think~ 라는 물음에 의견을 답한 것으로 보이며, 코파일럿에서 충분히 돌아간다고 생각하기 때문에 제가 기대한 주석을 돌려주지 않는 것 같습니다. 그러나 저는 사실 기존 코드의 주석이 충분히 친절하지 않다고 생각했기 때문에 ‘아니’라는 답을 가정하고 질문 (답정너 갈굼…) 한 것인데, 이런 방식의 대화법은 통하지 않나봅니다.


질문3:

gimme those annotations of the code you just provided - not of the latest answer - in Korean

방금 답변으로 준 그 “코드”의 주석을 한국어로 써달라고 했습니다. 드디어 제대로 알아듣고 기대한 결과를 보여주네요.

답변3: (아직 실제로 돌아가는지 확인은 해보지 못했습니다.)

function grantAccessToSheets() {
  // 외부 사용자의 이메일을 여기에 입력하세요
  var externalUserEmail = "[email protected]";

  // 공유하려는 시트의 ID 또는 이름을 배열로 입력하세요
  var sheetIdentifiers = ["Sheet1", "Sheet2", "Sheet3"];
  
  // 시트를 순회하면서 액세스 부여
  sheetIdentifiers.forEach(function(sheetIdentifier) {
    var sheet = getSheetByIdOrName(sheetIdentifier);
    if (sheet) {
      grantAccess(sheet.getId(), externalUserEmail);
      Logger.log("시트에 대한 액세스가 부여되었습니다: " + sheet.getName());
    } else {
      Logger.log("시트를 찾을 수 없습니다: " + sheetIdentifier);
    }
  });
}

// ID 또는 이름으로 시트를 가져오는 도우미 함수
function getSheetByIdOrName(sheetIdentifier) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetIdentifier);
  if (!sheet) {
    sheet = ss.getSheetById(sheetIdentifier);
  }
  return sheet;
}

// 외부 사용자에게 시트에 대한 보기 권한을 부여하는 도우미 함수
function grantAccess(sheetId, userEmail) {
  var sheetFile = DriveApp.getFileById(sheetId);
  sheetFile.addViewer(userEmail);
}



이번 시도는 chat GPT를 통해 업무에 직접적으로 활용할 수 있는 코드를 얻어냈다는 점에서 흡족하지만, 프롬프팅에 대해 탐구할 필요를 일깨워주는 계기였고, 사람과 대화하는 방식에 대해서도 생각해보게 만드네요. 실은 사람과 대화할 때에도 위에서 사용한 답정너 갈굼 방식은 바람직하지 않다고 생각하는 편인데, 역시 기계에게는 아직 무리인 것 같습니다.


4
2개의 답글

👉 이 게시글도 읽어보세요