2016-04-12 23 views
1

Birden çok boşluk yerini, bir Google Dokümanında bir komut dosyasıyla tekli olanlarla değiştirmeye çalışıyorum. Ama ne yazık ki verilen çözüm to this question benim için çalışmıyor. Gördüğünüz gibi çeşitli alternatifler denedim ama nasıl doğru yapacağımı anlayamıyorum. Herhangi bir fikir?Google Apps Komut Dosyası - .replace yöntemini kullanarak alanları kaldırın

function searchAndReplace() { 
 
    var body = DocumentApp.getActiveDocument() 
 
     .getBody(); 
 
    body.replaceText(/\s{2,}/,' '); 
 
    body.replaceText(/\s/g, " ") ; 
 
    body.replaceText("/\s/"," "); 
 
    body.replaceText('/\s{2,}/',' '); 
 
}

cevap

1

Dene:

function searchAndReplace() { 
    var body = DocumentApp.getActiveDocument().getBody(); 
    body.editAsText().replaceText('\\s*', ' '); 
} 

GÜNCELLEME

Bir seçeneğidir:

function getCorrections() { 
    var _getCorrections = 0, 
     text = DocumentApp.getActiveDocument().getBody().getText(), 
     regexp = /\s+/g, 
     matchesCorrections = text.match(regexp); 

    if (matchesCorrections) { 
    _getCorrections = matchesCorrections.reduce(function(previousValue, 
                 currentValue) { 
     return previousValue + currentValue.length - 1; 
    }, 0); 
    } 

    return _getCorrections; 
} 
+0

Cevabınız için teşekkürler. İşe yarıyor! Düzeltme sayısını gösteren bir rapor ekleyebilir misiniz? – Thoran

+0

@Thoran: Güncellenmiş cevaba bakınız. – wchiquito

+0

Bir tost iletisinde olduğu gibi düzeltme sayısını nasıl görüntüleyebilirim? ve bu işlevi menü çubuğuna nasıl eklerim? Cevap vermekten çekinmeyin, yine de lütuf vereceğim. 24 saat sürüyor. – Thoran

İlgili konular