Programing

Google 스프레드 시트에서 맞춤 함수로 검색 한 데이터 새로 고침

lottogame 2020. 10. 17. 08:59
반응형

Google 스프레드 시트에서 맞춤 함수로 검색 한 데이터 새로 고침


id웹 서비스 (가격)를 수신하고 정보를 가져 오는 맞춤 Google Apps 스크립트를 작성했습니다 .

이 스크립트를 스프레드 시트에서 사용하는데 잘 작동합니다. 내 문제는 이러한 가격이 변경되고 내 스프레드 시트가 업데이트되지 않는다는 것입니다.

스크립트를 다시 실행하고 셀을 업데이트하도록 강제 할 수 있습니까 (각 셀을 수동으로 이동하지 않고)?


좋아, 내 문제는 Google이 이상한 방식으로 작동하는 것 같습니다. 스크립트 매개 변수가 유사한 한 스크립트를 다시 실행하지 않고 이전 실행에서 캐시 된 결과를 사용합니다. 따라서 API에 다시 연결하지 않고 가격을 다시 가져 오지 않으며 단순히 캐시 된 이전 스크립트 결과를 반환합니다.

자세한 내용은 https://code.google.com/p/google-apps-script-issues/issues/detail?id=888을 참조하세요.

및 여기 : 업데이트되지 않는 데이터를 요약하는 스크립트

내 해결책은 스크립트에 다른 매개 변수를 추가하는 것이 었는데 사용하지도 않았습니다. 이제 이전 호출과 다른 매개 변수를 사용하여 함수를 호출하면 이러한 매개 변수의 결과가 캐시에 없기 때문에 스크립트를 다시 실행해야합니다.

따라서 함수를 호출 할 때마다 추가 매개 변수에 대해 "$ A $ 1"을 전달합니다. 또한 새로 고침이라는 메뉴 항목을 만들었고 실행하면 현재 날짜와 시간이 A1에 입력되므로 $ A $ 1을 두 번째 매개 변수로 사용하여 스크립트에 대한 모든 호출을 다시 계산해야합니다. 내 스크립트의 일부 코드는 다음과 같습니다.

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Refresh",
    functionName : "refreshLastUpdate"
  }];
  sheet.addMenu("Refresh", entries);
};

function refreshLastUpdate() {
  SpreadsheetApp.getActiveSpreadsheet().getRange('A1').setValue(new Date().toTimeString());
}

function getPrice(itemId, datetime) {
  var headers =
      {
        "method" : "get",
        "contentType" : "application/json",
        headers : {'Cache-Control' : 'max-age=0'}
      };

  var jsonResponse = UrlFetchApp.fetch("http://someURL?item_id=" + itemId, headers);
  var jsonObj = eval( '(' + jsonResponse + ')' );
  return jsonObj.Price;
  SpreadsheetApp.flush();
}   

그리고 ID가 5 인 항목의 가격을 셀에 넣으려면 다음 공식을 사용합니다.

=getPrice(5, $A$1)

가격을 새로 고침하려면 "새로 고침"-> "새로 고침"메뉴 항목을 클릭하면됩니다. onOpen()스크립트 를 변경 한 후 스프레드 시트를 다시로드해야합니다 .


나는 이것이 약간 오래된 질문이라는 것을 알고 있습니다. 그러나이 방법은 변경하는 것 외에는 사용자 작업이 필요하지 않습니다.

내가 한 일은 tbkn23과 비슷했습니다.

재평가하려는 함수에는 사용되지 않은 추가 매개 변수 $ A $ 1이 있습니다. 그래서 함수 호출은

=myFunction(firstParam, $A$1)

그러나 코드에서 함수 서명은

function myFunction(firstParam)

새로 고침 기능을 사용하는 대신 다음과 같은 onEdit (e) 기능을 사용했습니다.

function onEdit(e)
{
   SpreadsheetApp.getActiveSheet().getRange('A1').setValue(Math.random());
}

이 함수는 스프레드 시트의 셀이 편집 될 때마다 트리거됩니다. 이제 셀을 편집하고 난수를 A1에 배치하면 tbkn23이 제안한대로 매개 변수 목록이 새로 고쳐져 사용자 정의 함수가 다시 평가됩니다.


This is very late and I don't know it would be useful but actually there are setting here you can make NOW() update automatically

enter image description here


If you're custom function is inside a specific column simply order your spreadsheet by that column.

The ordering forces a refresh of the data which invokes your custom function for all rows of that column at once.


This may be a super old thread but may be useful for someone trying to do this, as I was just now.

Working off of Lexi's script as-is, it didn't seem to work anymore with the current Sheets, but if I add the dummy variable into my function as a parameter (no need to actually use it inside the function), it will indeed force google sheets to refresh the page again.

So, declaration like: function myFunction(firstParam,dummy) and then calling it would be as has been suggested. That worked for me.

Also, if it is a nuisance for a random variable to appear on all of your sheets that you edit, an easy remedy to limit to one sheet is as follows:

function onEdit(e)
{
  e.source.getSheetByName('THESHEETNAME').getRange('J1').setValue(Math.random());
}

Each time you deploy your web app it uses the same version. That's why it does not update.

Once you made changes go to File-> Manage versions and save new version. New version appears. Then when you deploy choose the the version you want to run enter image description here


If you have written a custom function and used it in your spreadsheet as a formula, then each time you open the spreadsheet or any referencing cell is modified, the formula is recalculated.

If you want to just keep staring at the spreadsheet and want its values to change, then consider adding a timed trigger that will update the cells. Read more about triggers here

참고URL : https://stackoverflow.com/questions/17341399/refresh-data-retrieved-by-a-custom-function-in-google-sheet

반응형