简体   繁体   中英

Apps Script: setting one formula for a whole column

I'm working on an Apps Script project for Sheets and I don't know if it's because I just never really worked with Sheets or Excel, but I don't know how to set a formula for a whole column through code.

var cell = sheet.getRange([i], 2);
var cell2 = sheet.getRange([i], 1);
var inhoud = cell2.getValue();
cell.setFormula("=(" + inhoud/86400000 + "DATE(1970,1,1)");

I want every B of a row to do something with the A of that same row. In the sheet self it's easy to just "drag the function down", to make it apply to every row, but I don't know how to get that to work in code as I can't use A2, for example, or A2:A30. Part of the problem may be that it's in a for loop:

var subsie = [];  

for (i = 0; i < subscriptions.length; i++) {

  var subscription = subscriptions[i];

  creationdate = subscription.creationTime;

  if (subscription.plan.planName == 'ANNUAL' && subscription.renewalSettings.renewalType == 'AUTO_RENEW') {  

  subsie.push([creationdate, ' ', subscription.plan.planName]);
  Logger.log(subsie);

    var cell = sheet.getRange([i], 2);
    var cell2 = sheet.getRange([i], 1);
    var inhoud = cell2.getValue();
    cell.setFormula("=(A1:A100/86400000) + DATE(1970,1,1)");

  } }

  sheet.getRange(sheet.getLastRow()+1, 1, subsie.length, subsie[0].length).setValues(subsie);

The actual goal is to convert the epoch values of A into dates, which I tried in a lot of different ways but turned out to be more difficult than I expected. This was the only formula that seemed to work for my output, which was like this: 1433235478178 . How can I make this code work? Thanks in advance!

解决了:)

creationdate = (subscription.creationTime/86400000)+25567;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM